Reputation: 67
I am trying to append new json object in existing json array of object. I am new in json. so please help me.
existing json :
{
"cluster":[
{
"path":"home/Nik",
"password":"welcome",
"isQueen":"true",
"host":"192.168.11.248",
"isQueenWorker":"true",
"user":"Nik"
}
]
}
new json :
{
"path":"home\/Nik",
"password":"welcome",
"isQueen":"true",
"host":"192.168.11.248",
"isQueenWorker":"true",
"user":"Nik"
}
I want to add new json to existing json array.
Upvotes: 0
Views: 792
Reputation: 79
you can directly append it
eg. first json
{"cluster":[{"path":"home/Nik","password":"welcome","isQueen":"true","host":"192.168.11.248","isQueenWorker":"true","user":"Nik"}]}
int i= cluster.length();
cluster[i]={"path":"home/Nik","password":"welcome","isQueen":"true","host":"192.168.11.248","isQueenWorker":"true","user":"Nik"}
Upvotes: 0
Reputation: 294
You may use it like below, you need to use push command to push an object inside an array.
var myObj = {
"cluster":[
{
"path":"home/Nik",
"password":"welcome",
"isQueen":"true",
"host":"192.168.11.248",
"isQueenWorker":"true",
"user":"Nik"
}
]
};
var x = {
"path":"home\/Nik",
"password":"welcome",
"isQueen":"true",
"host":"192.168.11.248",
"isQueenWorker":"true",
"user":"Nik"
};
alert(JSON.stringify(myObj))
var newArr = myObj.cluster;
newArr.push(x) //pushing object x in newArr. similarly you can add multiple objects in to it
var myJSON = JSON.stringify(newArr);
alert(myJSON)
Upvotes: 1