How Add a New Key/Value Pair to a Json Object

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

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)

How add a new Key/Value pair to a JSON Object?

Check for existence of a key in an object with the "in" operator

    var car = {make: 'Honda', model: 'Accord', year: 1998};        console.log(car);    console.log('make in car: ', 'make' in car);    // expected output: true        delete car.make;    console.log('removed make');    console.log(car);        console.log('add make in again, if not there');    if ('make' in car === false) {      car.make = 'Suzuki';    }        console.log('car.make : ', car.make);    // expected output: "Suzuki"

how to add key value pair in the JSON object already declared

Could you do the following:

obj = {
"1":"aa",
"2":"bb"
};


var newNum = "3";
var newVal = "cc";


obj[newNum] = newVal;



alert(obj["3"]); // this would alert 'cc'

How to add key value pair to the json?

You can do something like this:

var students = [ { city: 'California', company: 'ABC'}, { city: 'LA', company: 'PQR'}, { city: 'Mumbai', company: 'XYZ'}];        students.forEach((obj) => {  obj['email']= 'abc@xyz.com';  console.log(obj);});
// Final Outputconsole.log(students);

How to add a new key-value pair to each object in a JSON array

The only issue is with the placement of your print() statement. It should be outside of the for loop, and you should be printing entry['services'] rather than i. This allows you to print all of updated entries at once, rather than one at a time.

y = {"pin":110096}
entry = json.loads(json_var)

for i in entry['services']:
i.update(y)

print(json.dumps(entry['services']))

This outputs:

[{"service_name": "stack1", "service_version": "1", "pin": 110096},
{"service_name": "stack2", "service_version": "2", "pin": 110096},
{"service_name": "stack3", "service_version": "3", "pin": 110096}]

not able to add new key value pair in the JSON object kindly some one help me out

The code which you have pasted looks ok. but you are assigning the array to ABC property.

Remove the array and just assign the value.

for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = i
}

working example:

var object = [
{
"asdf": "",
"fdrtf": "869966",
"hdhfhfh": "utytut",
"Cat": "A"

},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"

},
{
"hjhj": "",
"hfhfhf": "h",
"hhfh": "hfjfhdj",
"cat": "B"
}
]

for (let i = 0; i < object.length; i++) {
object[i]['ABC'] = i
}

console.log(object);

//Use JSON.stringify if you need Json string
console.log(JSON.stringify(object));

Appending a key value pair to a json object

This is the easiest way and it's working to me.

var testJson = {
"name": "John Smith",
"age": 32,
"employed": true,
"address": {
"street": "701 First Ave.",
"city": "Sunnyvale, CA 95125",
"country": "United States"
},
"children": [
{
"name": "Richard",
"age": 7
},
{
"name": "Susan",
"age": 4
},
{
"name": "James",
"age": 3
}
]
};
testJson.collegeId = {"eventno": "6062","eventdesc": "abc"};

Add a new key-value pair to a JSON object



Related Topics



Leave a reply



Submit