Construct a JSON Payload in javascript

Nitesh Balusu
Giga Guru

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

Hi 

For that first you will have to construct a json whose last index would be the key which you are going to generate and put that in the main json, For ex - 

var test="200";
var json={};
json[''+test] = "ok";
gs.print(JSON.stringify(json));

 

Till test you will generate and then - 

mainjson.alliances.xyz.environments[''+env] = json;

 

The thing here is the last node should be the dynamic value to which you are going to assign some value.

 

Regards

Omkar Mone

I was able to do something like this, do you think this is a good idea? I couldn't work out the constructing part. But was able edit an existing one. Is it better to construct it from the start? I was unable to begin with your example.

 

var alliance = "123";
var env = "cef";
var team = "xxx";
var project = "xxyyzz";

var requestBody = 
	{
  "format_version": "0.2.19",
  "alliances": {
    "sample-alliance": {
      "environments": {
        "prd": {
          "teams": {
            "sample-team": {
              "action": "edit",
              "team": "dna",
              "projects": {
                "sample-project": {
                  "project": "sample-project",
                  "cost_center": "0",
                  "custom_iam_policies": [],
                  "iam": {
                    "view_group_email_name": "1412414214",
                    "sre_admin_group_email_name": "112414124123"
                  },
                  "allowed_apis": [
                    "24141541342",
                    "24324147194572"
                  ],
                  "networks": {
                    "sample-network": {
                      "flags": [
                        "VM"
                      ],
                      "region": "sampleregion",
                      "preferred-suffix" : "routable"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
};

requestBody.alliances[alliance] =  requestBody.alliances["sample-alliance"];

delete requestBody.alliances["sample-alliance"];

requestBody.alliances[alliance].environments[env] = requestBody.alliances[alliance].environments.prd;


delete requestBody.alliances[alliance].environments.prd;

requestBody.alliances[alliance].environments[env].teams[team] = requestBody.alliances[alliance].environments[env].teams.dna;

delete requestBody.alliances[alliance].environments[env].teams.dna;


requestBody.alliances[alliance].environments[env].teams[team].team = team;

requestBody.alliances[alliance].environments[env].teams[team].projects[project] =requestBody.alliances[alliance].environments[env].teams[team].projects["sample-project"];

delete requestBody.alliances[alliance].environments[env].teams[team].projects["sample-project"];

requestBody.alliances[alliance].environments[env].teams[team].projects[project].project = project;



gs.print(JSON.stringify(requestBody));