Delete json element

I want to remove a JSON element or one whole line from JSON.

I have a JSON string:

{ "result":[ { "FirstName": "Test1", "LastName": "User", }, { "FirstName": "user", "LastName": "user", }, { "FirstName": "Ropbert", "LastName": "Jones", }, { "FirstName": "hitesh", "LastName": "prajapti", } ] } 
+91
json javascript
Mar 15 '11 at 10:20
source share
9 answers
 var json = { ... }; var key = "foo"; delete json[key]; // Removes json.foo from the dictionary. 

You can use splice to remove elements from an array.

+158
Mar 15 '11 at 10:31
source share
— -

You have no commas in your JSON

UPDATE: you need to use array.splice and not delete if you want to remove elements from the array in the object

 var data = { "result": [{ "FirstName": "Test1", "LastName": "User" }, { "FirstName": "user", "LastName": "user" }] } console.log(data.result); console.log("------------ deleting -------------"); delete data.result[1]; console.log(data.result); // note the "undefined" in the array. data = { "result": [{ "FirstName": "Test1", "LastName": "User" }, { "FirstName": "user", "LastName": "user" }] } console.log(data.result); console.log("------------ slicing -------------"); var deletedItem = data.result.splice(1,1); console.log(data.result); // here no problem with undefined. 
+19
Mar 15 '11 at 10:28
source share

You can try to remove JSON as follows:

 var bleh = {first: '1', second: '2', third:'3'} alert(bleh.first); delete bleh.first; alert(bleh.first); 

Alternatively, you can also pass an index to remove the attribute:

 delete bleh[1]; 

However, to understand some of the consequences of using deletions, look here.

+11
Feb 27 '14 at 22:43
source share
  • Fix JSON errors: http://jsonlint.com/
  • Parse JSON (since you tagged the question using JavaScript, use json2.js )
  • Delete property from the object you created
  • Set the object back to JSON.
+6
Mar 15 2018-11-11T00:
source share

All the answers are great, and it will do what you ask too, but I believe the best way to delete this, and the best way for the garbage collector (if you use node.js) is like this:

 var json = { <your_imported_json_here> }; var key = "somekey"; json[key] = null; delete json[key]; 

This way the garbage collector for node.js will know that json['somekey'] no longer needed and will delete it.

+5
Jul 23 '13 at 17:34
source share

Try the following

 var myJSONObject ={"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; console.log(myJSONObject); console.log(myJSONObject.ircEvent); delete myJSONObject.ircEvent delete myJSONObject.regex console.log(myJSONObject); 
+5
Sep 06 '13 at 11:45
source share

As described by @mplungjan, I was right. Then I immediately press the speed increase button. But following this, I finally got an error.

 <script> var data = {"result":[ {"FirstName":"Test1","LastName":"User","Email":"test@test.com","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"}, {"FirstName":"user","LastName":"user","Email":"u@u.com","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"}, {"FirstName":"Ropbert","LastName":"Jones","Email":"Robert@gmail.com","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"}, {"FirstName":"hitesh","LastName":"prajapti","Email":"h.prajapati@zzz.com","City":"","State":"","Country":"","Status":"True","iUserID":"78"} ] } alert(data.result) delete data.result[3] alert(data.result) </script> 

Delete is just deleting the data, but the "place" still exists as undefined.

I did this and it works like a charm:

 data.result.splice(2,1); 

Value: delete 1 element at position 3 (because the array is counted in the form 0, then an element without 3 counts as 2)

+4
Sep 28 '16 at 17:02
source share

I recommend the splice method to remove an object from an array of JSON objects.

 jQuery(json).each(function (index){ if(json[index].FirstName == "Test1"){ json.splice(index,1); // This will remove the object that first name equals to Test1 return false; // This will stop the execution of jQuery each loop. } }); 

I use this because when I use the delete method, I get a null object after doing JSON.stringify(json)

+3
Sep 27 '17 at 5:37 on
source share

try it

 json = $.grep(newcurrPayment.paymentTypeInsert, function (el, idx) { return el.FirstName == "Test1" }, true) 
0
Jul 26 '19 at 4:14
source share



All Articles