0

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.

3
  • Do you use GSON by Google? Is quite helpful github.com/google/gson Commented Jun 7, 2018 at 9:58
  • Please add more information. This question is way too broad in its current state Commented Jun 7, 2018 at 10:06
  • please add more information, how do you expect json object look after appending new json? Commented Jun 7, 2018 at 10:30

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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"}

1 Comment

sorry a small change i= cluster.length; this definitely works ,it worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.