Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

merge json under another json objects

there is a json like that :

json1 = `
{
  "webAPIModification": "`+ new Date() + `",
  "AppSetting": {
    "setting-toastSound": true,
    "setting-animMode": true,
    "setting-landscape": "NAN"
  },
  "UserInfo": {
    "OS": "NAN"
  }
}`;

now i have another json like this:

json2 = `
{
  "ver": 1,
  "send": true,
  "status": "NAN"
}`;

how can add json2 into the json1 (under UserInfo) ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

json1 = `
{
  "webAPIModification": "`+ new Date() + `",
  "AppSetting": {
    "setting-toastSound": true,
    "setting-animMode": true,
    "setting-landscape": "NAN"
  },
  "UserInfo": {
    "OS": "NAN",

    "ver": 1, <--> what i need
    "send": true, <--> what i need
    "status": "NAN" <--> what i need

  }
}`;

how i can do that in jquery or javascript way???

>Solution :

A low level solution could be: use JSON.parse(jsonstring) in order to have objects and treat them as objects with properties or arrays and then you can set what you wan… after that you can get the json string with JSON.stringify(object).

let obj = JSON.parse(json1);
let otherUserInfo = JSON.parse(json2);

for (var index in otherUserInfo){
     obj.UserInfo[index] = otherUserInfo[index];
}

json1 = JSON.stringify(obj);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading