Open In App

How to delete an index from JSON Object ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the methods to delete any index from a JSON Object.

What is JSON?

JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used when data is sent from a server to a web page. JSON is “self-describing” and easy to understand. It is the replacement of the XML data exchange format. It is easy to struct the data compare to XML. It supports data structures like arrays and objects and JSON documents that are rapidly executed on the server.

JSON syntax rule –

  • Data should be in key : value pairs
  • Data is separated by comma(‘,’)
  • Curly brackets { }  holds object.
  • Square brackets [ ] holds array.

What is it used for?

Like XML, it is one of the ways of formatting the data. Such a format of data is used when data is sent from a server to a web page.

Prerequisite – 

  • Node installed

Syntax –

let json_object = [{
    "property_1" : "property_value_1",
    "property_2" : "property_value_2"
},
{
    "property_1" : "property_value_1",
    "property_2" : "property_value_2",
    "property_3" : "property_value_3"
}]

Now, to delete any index from this JSON object, we will learn different methods that are explained below – 

Using Splice() Method: This method is used to modify the contents by removing the existing elements and/or by adding new elements.

Here, we will look only, at how can we use splice to remove elements.

Syntax:  

Object.splice(index, remove_count )

Example 1: In this example, we will remove the element at the first index of the object.

Javascript




let Data_Structures = [
    {
        "Name": "Trees",
        "Course": "Introduction of Trees",
        "Content": ["Binary Tree", "BST",
            "Generic Tree"]
    },
    {
        "Name": "Graphs",
        "Topics": ["BFS", "DFS", "Topological Sort"]
    }
]
 
console.log("Object Before deleting : ");
console.log(Data_Structures);
console.log("Using splice  for Deleting object at index 0");
Data_Structures.splice(0, 1);
console.log("Object Post  Deleting : ");
console.log(Data_Structures);


How to run this?

  • Open the command prompt.
  • Go to the directory where your JSON file is saved by using cd command.
  • Now simply type the command 
node file_name

Output:

Object Before deleting : 
[
  {
    Name: 'Trees',
    Course: 'Introduction of Trees',
    Content: [ 'Binary Tree', 'BST', 'Generic Tree' ]
  },
  { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] }
]
Using splice  for Deleting object at index 0
Object Post  Deleting : 
[ { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] } ]

Using delete property: This keyword is used to delete the property as well as its associated value. Post deletion, the deleted property can not be retrieved back.

Syntax:

delete object.property or
delete object['property'] or
delete object[index]

Return value: It returns true for all cases and returns false when the property is an own non-configurable property.

Example 2: We will use the delete keyword for deleting an object as well as its property. 

At first, we will delete an object at the first index and then delete the property of the object at index 1.

Javascript




let students = [
    {
        "student": "Akshit",
        "address": "Moradabad",
        "phone": "98760"
    },
    {
        "student": "Nikita",
        "address": "Lucknow",
        "phone": "98754"
    },
    {
        "student": "Somya",
        "address": "Delhi",
        "phone": "67878"
    },
    {
        "student": "Eshika",
        "address": "Bangalore",
        "phone": "67676"
    },
    {
        "student": "Parul",
        "address": "Chennai",
        "phone": "77668"
    }
]
console.log("Object Before deleting : ");
console.log(students);
console.log("Using Delete Keyword for "
    + "Deleting object at index 0");
delete students[0];
console.log("Object Post  Deleting : ");
console.log(students);
console.log("Using Delete Keyword for "
    + "Deleting object property at index 1");
delete students[1].phone;
console.log("Object Post  Deleting : ");
console.log(students);


Output:

Object Before deleting : 
[
  { student: 'Akshit', address: 'Moradabad', phone: '98760' },
  { student: 'Nikita', address: 'Lucknow', phone: '98754' },
  { student: 'Somya', address: 'Delhi', phone: '67878' },
  { student: 'Eshika', address: 'Bangalore', phone: '67676' },
  { student: 'Parul', address: 'Chennai', phone: '77668' }
]
Using Delete Keyword for Deleting object at index 0
Object Post  Deleting :
[
  <1 empty item>,
  { student: 'Nikita', address: 'Lucknow', phone: '98754' },
  { student: 'Somya', address: 'Delhi', phone: '67878' },
  { student: 'Eshika', address: 'Bangalore', phone: '67676' },
  { student: 'Parul', address: 'Chennai', phone: '77668' }
]
Using Delete Keyword for Deleting object property at index 1
Object Post  Deleting :
[
  <1 empty item>,
  { student: 'Nikita', address: 'Lucknow' },
  { student: 'Somya', address: 'Delhi', phone: '67878' },
  { student: 'Eshika', address: 'Bangalore', phone: '67676' },
  { student: 'Parul', address: 'Chennai', phone: '77668' }
]

Using null: When we set the value of any object to null, then the garbage collector of the node automatically deletes the property value. However, the name of the property remains.

Syntax:

object.property=null

Example 3: In this example, we will delete an object property by using null.

Javascript




let Data_Structures = [
    {
        "Name": "Trees",
        "Course": "Introduction of Trees",
        "Content": ["Binary Tree", "BST",
            "Generic Tree"]
    },
    {
        "Name": "Graphs",
        "Topics": ["BFS", "DFS", "Topological Sort"]
    }
]
 
console.log("Object Before deleting : ");
console.log(Data_Structures);
console.log("Using null  for Deleting object at index 0");
Data_Structures[0].Content = null;
console.log("Object Post  Deleting : ");
console.log(Data_Structures);


Output:

Object Before deleting : 
[
  {
    Name: 'Trees',
    Course: 'Introduction of Trees',
    Content: [ 'Binary Tree', 'BST', 'Generic Tree' ]
  },
  { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] }
]
Using null  for Deleting object at index 0
Object Post  Deleting :
[
  { Name: 'Trees', Course: 'Introduction of Trees', Content: null },
  { Name: 'Graphs', Topics: [ 'BFS', 'DFS', 'Topological Sort' ] }
]


Last Updated : 02 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads