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);
1
  • stackoverflow.com/questions/3153969/… 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. Commented Nov 19, 2019 at 5:41

2 Answers 2

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' }
Sign up to request clarification or add additional context in comments.

4 Comments

stackoverflow.com/questions/3153969/… 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.
object[variable] = value; // if your variable has value test, you will have a object like this {test: 'your value' }
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?
Thats assigning the existing values in xyz to the newly created key.
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;

2 Comments

stackoverflow.com/questions/3153969/… 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.
See Updated answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.