Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new key value pair in existing JSON object using JavaScript?

Tags:

var json = {     "workbookInformation": {         "version": "9.1",         "source-platform": "win"     },     "datasources1": {         ...     },     "datasources2": {         ...     } } 

I need to add new key pair under workbookInformation like

var json={     "workbookInformation": {          "version": "9.1",          "source-platform": "win",          "new_key":"new_value"     },     "datasources1": {         ...     },     "datasources2": {         ...     } } 

json['new_key'] = 'new_value'; adds the new key but I want it under "workbookInformation"

like image 835
T Sooraj Avatar asked Jan 18 '17 05:01

T Sooraj


People also ask

How do I add a key-value to an array of objects?

To add a key/value pair to all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use dot notation to add a key/value pair to the current object. The key/value pair will get added to all objects in the array.

How do I add data to an existing JSON object?

Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .

How does JSON create key-value pairs?

In order to set a key-value pair in a KiiObject, call the set() method of the KiiObject class. The set() method has an overloaded version for each data type. Specify a key-value pair as arguments of the set() method. The specified key-value pair will be saved at the first level of the JSON document hierarchy.

How to add a new key value pair in existing JSON?

How to add a new key value pair in existing JSON object using JavaScript? To add a new key value pair in existing JSON object using JavaScript, we can parse the JSON string with JSON.parse, then add the key-value pairs we want, and then convert the object back to a JSON string with JSON.stringify.

How to add properties or Insert key-value pair in JavaScript Object?

To add properties or insert key-value pair in JavaScript Object, use either dot (.) notation or square bracket notation ([ ]). To create an Object in JavaScript, use the { } notation and add the key/value pairs to that Object.

What are key-value pairs in an object?

An object contains properties, or key-value pairs. The desk object above has four properties. Each property has a name, which is also called a key, and a corresponding value. For instance, the key height has the value "4 feet". Together, the key and value make up a single property.

How to delete a key-value pair in JavaScript?

How to Delete a Key-Value Pair in JavaScript. To delete a key-value pair use the delete operator. This the syntax: delete objectName.keyName. So to delete the height key and its value from the basketballPlayer object, you’d write this code: delete basketballPlayer.height; As a result, the basketballPlayer object now has three key-value pairs.


1 Answers

There are two ways for adding new key value pair to Json Object in JS

var jsObj = {     "workbookInformation": {         "version": "9.1",         "source-platform": "win"     },     "datasources1": {      },     "datasources2": {      } } 

1.Add New Property using dot(.)

jsObj.workbookInformation.NewPropertyName ="Value of New Property";  

2.Add New Property specifying index like in an arrays .

jsObj["workbookInformation"]["NewPropertyName"] ="Value of New Property";  

Finally

 json = JSON.stringify(jsObj);  console.log(json) 
like image 60
Arif H-Shigri Avatar answered Sep 30 '22 19:09

Arif H-Shigri