Construct a JSON Payload in javascript

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2019 05:42 PM
Hello!
I have to construct a JSON payload that looks like this, can someone help me? I am able to get the straight forward one but unable to build a nested payload. How do I go about adding more nested keys, one inside the other. Also some of the keys and values are dynamic and have to replaced with variables.
https://community.servicenow.com/community?id=community_question&sys_id=26807b5cdbfcd34c7b337a9e0f961937
This link helps a little but unable to understand how to build keys one within another.
{
"format_version": "0.2.19",
"alliances": {
"xyz": {
"environments": {
"prd": {
"teams": {
"abc": {
"action": "edit",
"team": "abc",
"projects": {
"prjabc": {
"project": "prjabc",
"cost_center": "0",
"custom_iam_policies": [],
"iam": {
"view_group_email_name": "abc@email.com",
"sre_admin_group_email_name": "xyz@email.com"
},
"allowed_apis": [
"api1",
"api2"
],
"networks": {
"network1": {
"flags": [
"VM"
],
"region": "sample-region",
"preferred-suffix": "routable"
}
}
}
}
}
}
}
}
}
}
}
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 07:18 PM
I script I tried won't work, looks like I need help in how to start.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 08:36 PM
Hi Nitesh,
Please check the below code, i took your json provided in the question for building the JSON -
var json =
{
"format_version": "",
"alliances": {
"xyz": {
"environments": {
"prd": {
"teams": {
"abc": {
"action": "",
"team": "",
"projects": {
"prjabc": {
"project": "",
"cost_center": "",
"custom_iam_policies": "",
"iam": {
"view_group_email_name": "",
"sre_admin_group_email_name": ""
},
"allowed_apis": "",
"networks": {
"network1": {
"flags":"",
"region": "",
"preferred-suffix": ""
}
}
}
}
}
}
}
}
}
}
};
var customIAM = [];
var allowedApi = ["api1","api2"];
var flaged = ["VM"];
json.format_version = "0.2.19";
json.alliances.xyz.environments.prd.teams.abc.action = "edit";
json.alliances.xyz.environments.prd.teams.abc.team = "abc";
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.project = "prjabc";
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.cost_center = "0";
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.custom_iam_policies = customIAM;
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.iam.view_group_email_name = "abc@email.com";
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.iam.sre_admin_group_email_name = "xyz@email.com";
json.alliances.xyz.environments.prd.teams.abc.projects.allowed_apis = allowedApi;
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.networks.network1.flags = flaged;
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.networks.region = "sample-region";
json.alliances.xyz.environments.prd.teams.abc.projects.prjabc.networks.preferred_suffix = "routable";
var mainJson = JSON.stringify(json);
gs.print(mainJson);
Hope this helps.
Regards
Omkar Mone

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 08:54 PM
Hi Omkar,
Thank you for your reply. This code works when the keys are hardcoded. The problem I am facing is, the keys have to be dynamic and the values under keys in most cases have to be dynamic.
For example:
var env = current.variables.vpc_environment_type;
var env should replace the value abc in the script. So if env contains 123, the key becomes 123. Like below.
"alliances": {
"123": {
"environments": {
How do I make the keys dynamic?
json.alliances.xyz.environments[env].teams.abc.action = "edit";
I cannot do something like this as it throws an error, I guess keys store values in them so we have to add a new key and delete the old one which is not working very well.
May be my question should say, how to construct a JSON object with dynamic keys?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 09:14 PM
Hi
Creating dynamic key would go something like this,
PS - Tried and works in my PDI -
Need to put single quotes ''
var test="200";
var json={};
json[''+test] = "ok";
gs.print(JSON.stringify(json));
Output -
*** Script: {"200":"ok"}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2019 09:25 PM
Thank you for trying, I was able to get there but do you know how to build them like a nest? Object within an object like below?
"alliances": {
"xyz": {
"environments": {
"prd": {
"teams": {