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"
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.
Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .
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 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.
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.
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. 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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With