Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript : add/remove json element in json object

I need to modify my JSON Object by add/remove Json element. Here is my JSON Object,

var candidate = {  
   "name":"lokesh",
   "age":26,
   "skills":[  
      "Java",
      "Node Js",
      "Javascript"
   ]
};

I need to remove the element "skills" and output should be like,

{  
   "name":"lokesh",
   "age":26
}

And I again I need to add the element "skills" as string and the output should be like,

{  
   "name":"lokesh",
   "age":26,
   "skills":"javascript"
}

Please help,

Thanks in advance.

like image 385
lokeshkumar Avatar asked Jun 24 '16 09:06

lokeshkumar


People also ask

How do I remove a specific element from a JSON array?

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.

What is toJSON () in JSON?

The toJSON() method returns a string representation of the Date object.

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

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

How remove slashes from JSON in Java?

String jsonFormattedString = jsonStr. replaceAll("\\", "");


1 Answers

Other ways it can be achieved.

For Adding:

candidate["skills"] = "javascript";

For Deleting:

var skill = "javascript";
delete candidate[skill];

or

delete candidate.skills;
like image 85
Praveen Avatar answered Oct 28 '22 22:10

Praveen