Skip to content
Related Articles

Related Articles

How to delete an index from JSON Object ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 28 Mar, 2022

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 the 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 –

var 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.
  • Using Delete property
  • Using null

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 )

Parameter: This method accepts two or three parameters, some of them are described below:  

index: It is a required parameter. This parameter is the index that starts modifying the array (with origin at 0). This can be negative also, which begins after that many elements, counting from the end.

remove_count: The number of elements to be removed from the starting index.

Return Value: While it mutates the original array in-place, still it returns the list of removed items. In case there is no removed array, it returns an empty array.

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

Javascript




<script>
    var 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);
</script>

How to run this?

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

Output:

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]

Parameters:

object: It specifies the name of an object,
property: .It specifies the property to delete.

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




<script>
    var 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);
</script>

Output:

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




<script>
    var 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);
</script>

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!