Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove key-value pair from JSON object

I have this JSON object below;

[     {         XXX: "2",         YYY: "3",         ZZZ: "4"     },     {         XXX: "5",         YYY: "6",         ZZZ: "7"     },     {         XXX: "1",         YYY: "2",         ZZZ: "3"     } ] 

I want to remove the YYY key-value from the json object such that the new json object will look like this;

[     {         XXX: "2",                ZZZ: "4"     },     {         XXX: "5",                ZZZ: "7"     },     {         XXX: "1",                ZZZ: "3"     } ] 

I tried delete jsonObject['YYY'] but this is not correct. How can this be done in javascript? Thank you.

like image 426
guagay_wk Avatar asked Jul 16 '14 01:07

guagay_wk


People also ask

How do you remove a key-value pair from a JSON object?

JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.

How do you remove a key-value pair from a JSON array?

To remove JSON object key and value with JavaScript, we use the delete operator.

How do I remove a key from a JSON object?

To remove JSON element, use the delete keyword in JavaScript.

How do you remove a key-value pair from a JSON object in Python?

To delete a JSON object from a list: Parse the JSON object into a Python list of dictionaries. Use the enumerate() function to iterate over the iterate over the list. Check if each dictionary is the one you want to remove and use the pop() method to remove the matching dict.


1 Answers

What you call your "JSON Object" is really a JSON Array of Objects. You have to iterate over each and delete each member individually:

for(var i = 0; i < jsonArr.length; i++) {     delete jsonArr[i]['YYY']; } 
like image 65
Justin Niessner Avatar answered Sep 22 '22 17:09

Justin Niessner