Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove element from JSON Object

I have a json array which looks something like this:

  {     "id": 1,     "children": [     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     },     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     },     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     },     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     },     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     },     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     },     {         "id": 2,         "children": {             "id": 3,             "children": {                 "id": 4,                 "children": ""             }         }     }] } 

I would like to have a function which removes the elements which has the "children" empty. How can I do it? I am not asking for the answer, only suggestions

like image 844
Stack Overfolow Avatar asked Mar 16 '13 15:03

Stack Overfolow


People also ask

How do you remove an element from a JSON object?

To remove item from json object in javascript, use delete keyword it will remove key value pair from object only you have to mantion delete keyword with key.

How do you remove an element from a JSON file in Java?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

How do I remove a field from a JSON object in Python?

Firstly just open the JSON file and load the data. Then to check if the key "data" and "UserName" are in it or not. If yes, delete these keys and their value.


1 Answers

To iterate through the keys of an object, use a for .. in loop:

for (var key in json_obj) {     if (json_obj.hasOwnProperty(key)) {         // do something with `key'     } } 

To test all elements for empty children, you can use a recursive approach: iterate through all elements and recursively test their children too.

Removing a property of an object can be done by using the delete keyword:

var someObj = {     "one": 123,     "two": 345 }; var key = "one"; delete someObj[key]; console.log(someObj); // prints { "two": 345 } 

Documentation:

  • https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete
like image 187
Lekensteyn Avatar answered Sep 16 '22 21:09

Lekensteyn