1

In the JSON below, I want replace the KEYS xyz and abc with the dynamic values of alliance and env that I am getting from the form through the below statements, replacing the words abc in quotes with straight object names below is giving me an error. Any way to achieve this?

var env = g_form.getValue('vpc_environment_type');
var alliance = g_form.getValue('alliance_business_unit');
var team = g_form.getVal`enter code here`ue('alliance_segment_team_name_df_ingestion');
var project = g_form.getValue('project_name_gcp_df_ingestion_npe');

var requestBody =   {
  "format_version": "0.2.19",
  "alliances": {
    "xyz": {
      "environments": {
        "abc": {
          "teams": {
            "dna": {
              "action": "edit",
              "team": "dna",
              "projects": {
                "xxxx": {
                  "project": "xxxxx",
                  "cost_center": "0",
                  "custom_iam_policies": [],
                  "iam": {
                    "dev_group_email_name": "123",
                    "view_group_email_name": "456",
                    "sre_admin_group_email_name": "789"
                  },
                  "allowed_apis": [
                    "123",
                    "123"
                  ],
                  "networks": {
                    "xxxxx": {
                      "flags": [
                        "VM"
                      ],
                      "region": "123,
                      "preferred-suffix" : "123"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
};

requestBody = new global.JSON().encode(requestBody);
console.log(requestBody);
rrk
  • 15,677
  • 4
  • 29
  • 45
  • https://stackoverflow.com/questions/3153969/create-object-using-variables-for-property-name Is something like this possible? I do not want to replace anything, I want it to be like that from the start, the JSON I posted is an example, I want to replace some keys with variables so that I can get a different JSON payload for every form that's submitted by the user. – Nitesh Chowdhary Nov 19 '19 at 05:41

2 Answers2

1

Try something like this. Be aware that you cannot just rename a key in a JavaScript object, you need to add a new key with new name and delete old key if not required. Also be aware when you delete old key you may delete the values in new keys if data is mutated.

var alliance = g_form.getValue('alliance_business_unit');
requestBody.alliances[alliance] =  requestBody.alliances.xyz;

this will add new key under alliance with key name thats returned from g_form.getValue('alliance_business_unit');

Similarly do it for env also.

Dynamic assignment is done like this

object[variable] = value; 

If your variable has value test, you will have a object like this

{test: 'your value' }
kiranvj
  • 32,342
  • 7
  • 71
  • 76
  • https://stackoverflow.com/questions/3153969/create-object-using-variables-for-property-name Is something like this possible? I do not want to replace anything, I want it to be like that from the start, the JSON I posted is an example, I want to replace some keys with variables so that I can get a different JSON payload for every form that's submitted by the user. – Nitesh Chowdhary Nov 19 '19 at 05:42
  • `object[variable] = value;` // if your variable has value test, you will have a object like this `{test: 'your value' }` – kiranvj Nov 19 '19 at 05:51
  • I am confused requestBody.alliances[alliance] = requestBody.alliances.xyz; Shouldn't it be the other way around? Aren't we replacing xyz with variable alliance? Could you explain on this please? – Nitesh Chowdhary Nov 19 '19 at 06:09
  • Thats assigning the existing values in xyz to the newly created key. – kiranvj Nov 19 '19 at 06:39
0

Stringify > Replace > Convert to JSON

You need to replace the Strings: Dynamic1 and Dynamic2 with your desired keys.

requestBody = JSON.parse(JSON.stringify(requestBody).replace("\"xyz\"","\"Dynamic1\"").replace("\"abc\"","\"Dynamic2\""));

Updated: You can use variables as keys in this manner.

var newKey1 = "DynamicKey1";
requestBody.alliances[newKey1] = requestBody.alliances.xyz;

var newKey2 = "DynamicKey2";
requestBody.alliances[newKey1].environments[newKey2] = requestBody.alliances[newKey1].environments.abc;
Mahbub Moon
  • 491
  • 2
  • 8
  • https://stackoverflow.com/questions/3153969/create-object-using-variables-for-property-name Is something like this possible? I do not want to replace anything, I want it to be like that from the start, the JSON I posted is an example, I want to replace some keys with variables so that I can get a different JSON payload for every form that's submitted by the user. – Nitesh Chowdhary Nov 19 '19 at 05:42
  • See Updated answer – Mahbub Moon Nov 19 '19 at 06:02