简体   繁体   中英

Adding Nested Key To JSON Using Javascript

I'm just starting to learn Javascript so my apologies if this is a stupid question.

Basically my issue is this:

Via an external form that I cannot change, I am receiving the following data.

var data = "{\"status\": \"created\", \"name\": \"mike\", \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\", \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\" }"

Before I can send the data via API, the documentation requires that I add an additional nested key (templateRoles) to wrap rolename, emailsubject, and email into a new array. The final results is suppose to look like this.

var data = "{\"status\": \"created\", \"name\": \"mike\", \"templateRoles\": [{ \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\"}], \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\"}"

For the life of me I cannot figure out how to do this. I've tried using splice and replace but either I am doing it wrong or that isn't the right approach. Any suggestions or pointers in the right direction would be greatly appreciated.

Thanks in advance.

Use JSON.parse(data) to convert your JSON string into an object. Then you can manipulate that object. Once you're done manipulating simply use JSON.stringify() to get a JSON string back.

Sample code:

var data = "{\"status\": \"created\", \"name\": \"mike\", \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\", \"templateRoles\": [{ \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\"}]}";

var jsonObj = JSON.parse(data);
jsonObj.templateRoles = "some roles here";

var newDataStr = JSON.stringify(jsonObj);

So your basic code would look like this. Make the JSON string a JavaScript object, then manipulate it as such. Afterwards you can use JSON.stringyfi to put back to a JSON string.

var jsonData = "{\"status\": \"created\", \"name\": \"mike\", \"templateId\": \"0171502E-38F7-43A7-BA09-6FC1FDAB09C2\", \"roleName\": \"Signer\", \"emailSubject\": \"test\", \"email\": \"hellO@yahoo.com\"}";

var parsedJson = JSON.parse(jsonData);

// now manipulate

parsedJson.newProperty = "hey there";

parsedJson.status = "not created";

As a side note, I don't think you can order the keys, so you can do whatever in terms of adding properties or functions. Never rely on the keys to be in order. And by key, I mean property of function name of the object. Objects are held in a dictionary type way where their keys hold information.

So:

parsedJson["created"]

Is the same as

parsedJson.created

You can conditionally add data depending on whether a key exists:

for (var key in parsedJson)
    if (key === "someKey")
        // do something

OR

if (parsedJson["someKey"] === undefined)
    // do something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM