JSON stands for JavaScript Object Notation. A JSON object is really a string that has yet to be turned into the object it represents.
To add a property to an existing object in JS you could do the following.
object["property"] = value;
or
object.property = value;
If you provide some extra info like exactly what you need to do in context you might get a more tailored answer.
Answer from Quintin Robinson on Stack OverflowJSON stands for JavaScript Object Notation. A JSON object is really a string that has yet to be turned into the object it represents.
To add a property to an existing object in JS you could do the following.
object["property"] = value;
or
object.property = value;
If you provide some extra info like exactly what you need to do in context you might get a more tailored answer.
var jsonObj = {
members:
{
host: "hostName",
viewers:
{
user1: "value1",
user2: "value2",
user3: "value3"
}
}
}
for(var i=4; i<=8; i++){
var newUser = "user" + i;
var newValue = "value" + i;
jsonObj.members.viewers[newUser] = newValue ;
}
console.log(jsonObj);
Videos
JSON is just a notation; to make the change you want parse it so you can apply the changes to a native JavaScript Object, then stringify back to JSON
var jsonStr = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';
var obj = JSON.parse(jsonStr);
obj['theTeam'].push({"teamId":"4","status":"pending"});
jsonStr = JSON.stringify(obj);
// "{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}"
var Str_txt = '{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}';
If you want to add at last position then use this:
var parse_obj = JSON.parse(Str_txt);
parse_obj['theTeam'].push({"teamId":"4","status":"pending"});
Str_txt = JSON.stringify(parse_obj);
Output //"{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"},{"teamId":"4","status":"pending"}]}"
If you want to add at first position then use the following code:
var parse_obj = JSON.parse(Str_txt);
parse_obj['theTeam'].unshift({"teamId":"4","status":"pending"});
Str_txt = JSON.stringify(parse_obj);
Output //"{"theTeam":[{"teamId":"4","status":"pending"},{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"3","status":"member"}]}"
Anyone who wants to add at a certain position of an array try this:
parse_obj['theTeam'].splice(2, 0, {"teamId":"4","status":"pending"});
Output //"{"theTeam":[{"teamId":"1","status":"pending"},{"teamId":"2","status":"member"},{"teamId":"4","status":"pending"},{"teamId":"3","status":"member"}]}"
Above code block adds an element after the second element.
var data = []
function Client(date, contact) {
this.date = date
this.contact = contact
}
clients = new Array();
for (i = 0; i < 4; i++) {
clients.push(new Client("2018-08-0" + i, i))
}
for (i = 0; i < clients.length; i++) {
var dict = {}
dict['Date'] = clients[i].date
dict['Contact'] = clients[i].contact
data[i] = dict
}
console.log(data)
It's a simple push object to array operation. Please try below
var data=[];
var i;
for (i = 0; i < clients.length; i++) {
data.push({
date:clients.date,
contact:clients.contact
});
}
Like 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)