Like this
Copyjsondata.FDamount = 'false';
// or
jsondata['FDamount'] = 'false';
Answer from senK on Stack OverflowLike this
Copyjsondata.FDamount = 'false';
// or
jsondata['FDamount'] = 'false';
Simply do this :
Copyjsondata['FDamount'] = 'false';
jsondata['DDamount'] = 'true';
Or this :
Copyjsondata.FDamount = 'false';
jsondata.DDamount = 'true';
By the way, you define boolean as string, the correct way should be :
Copyjsondata['FDamount'] = false;
jsondata['DDamount'] = true;
To push a little bit further, you can use jQuery.extend to extend the original var, like this :
CopyjQuery.extend(jsondata, {'FDamount': 'false', 'DDamount': 'true'});
// Now, jsondata will be :
{"all":"true","FDamount":"false","DDamount":"true"}
jQuery.extend is available when using jQuery (of course), but I'm sure you can find similar methods in other libraries/frameworks.
(I'm using single quotes, but double quotes works too)
apex - Adding a new key value pair in the JSON array - Salesforce Stack Exchange
How to add a new key value pair in existing JSON object using JavaScript? - Stack Overflow
javascript - Add a json value to an existing key's value - Salesforce Stack Exchange
javascript - not able to add the key value pair properly in JSON object - Salesforce Stack Exchange
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'
You can use dot notation or bracket notation ...
var obj = {};
obj = {
"1": "aa",
"2": "bb"
};
obj.another = "valuehere";
obj["3"] = "cc";
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 want to add new key and value to each of the key of json object and then you can use the following code else you can use the code of other answers -
Object.keys(json).map(
function(object){
json[object]["newKey"]='newValue'
});
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"};
You need to make an object at reference "collegeId", and then for that object, make two more key value pairs there like this:
var concattedjson = JSON.parse(json1);
concattedjson["collegeId"] = {};
concattedjson["collegeId"]["eventno"] = "6062";
concattedjson["collegeId"]["eventdesc"] = "abc";
Assuming that concattedjson is your json object. If you only have a string representation you will need to parse it first before you extend it.
Edit
demo for those who think this will not work.
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']= '[email protected]';
console.log(obj);
});
// Final Output
console.log(students);
I would suggest you simply use a for loop for each element in your array and since it's JSON simply specify a new "key = value" inside the current element.
edit : here's an example :
var entreprises = [
{id: 33, uuid: "3d103130-ae0d-11e9-8e6c-dfb1a3a5afce", nom: "test", adresse: "test", ville: "tes" },
{id: 34, uuid: "81524090-ae0d-11e9-8894-2326c7695f11", nom: "test2", adresse: "test2", ville: "test2"}];
for (let i = 0; i < entreprises.length; i++) {
entreprises[i].key = "value";
}
(Note that this has nothing to do with JSON. You're not using JSON there, you're using an object initializer. JSON is a textual (not code) format, which is a subset of JavaScript's object initializer syntax.)
Do it outside the object initializer, using [] notation:
var id = $('#myInput').val();
var post = {};
post[product[id]] = 'myValue';
That will take the value (at runtime) of product[id] and use that as the key for the property. If you wanted the key to literally be product[123] when id is 123, you'd use this instead:
post['product[' + id + ']'] = 'myValue';
A more generic discussion:
var a = "foo";
var obj = {};
obj[a] = "bar";
console.log(obj.foo); // "bar"
JavaScript allows you to specify property keys in two ways: Using dotted notation and a literal (obj.foo), or using bracketed notation and a string (obj["foo"]). In the latter case, the string doesn't have to be a string literal, it can be the result of any expression.
Try
post['product[' + id + ']'] = 'myValue';