# Add parameter to JSON Body PUT Request while retaining parameters from GET request

**URL:** <https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310>\
**Category:** Help Hub\
**Created:** [August 21, 2019, 3:04pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310 "2019-08-21T15:04:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDrozdz17](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mdrozdz17/32/2605_2.png) [@MDrozdz17](https://community.postman.com/u/MDrozdz17)\
**Post date:** [August 21, 2019, 3:04pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/1 "2019-08-21T15:04:50Z")

</div>

I am trying to automate a task that adds a field to the request body in my PUT request.

Basically the first thing i do is a GET request to see what the request body looks like:  
{  
“jobName-0”: “/uscm-claims-training/job/boley-dynamic-secrets”,  
“isTag-0”: “false”,  
“triggers-0”: “add;prmerged;push;manual;delete;”,  
“token-0”: “”,  
“buildParameters-0”: “”,  
“branchRegex-0”: “”,  
“pathRegex-0”: “”  
}

Now that i have that information I want to add a field (see below) while retaining all other values from the GET request so I want to add basically: this field:  
**"jenkinsServer-0": “USCM-CLAIMS-TRAINING”,**

Is there a way to add a field to this? Each GET request has different values so I can’t just use the same body for each project.

Matt

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 21, 2019, 4:01pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/2 "2019-08-21T16:01:45Z")

</div>

Hi @MDrozdz17, welcome to the community! 👋

You can make use of [variables in Postman](https://learning.getpostman.com/docs/postman/environments_and_globals/variables/)!

All you need to do is write the following in the test script:

```javascript
// Get the response as JSON
let response = pm.response.json();

// Add the key to the object
response["jenkinsServer-0"] = “USCM-CLAIMS-TRAINING”;

// Set the whole response to a variable after stringifying it
pm.globals.set('jobResponse', JSON.stringify(response));

```

Now, in the request body of your PUT request, just write the following:

```auto
{{jobResponse}}

```

This should help you out.  
Let me know if you have any doubts.

---

<div class="post-metadata">

**Author:** ![MDrozdz17](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mdrozdz17/32/2605_2.png) [@MDrozdz17](https://community.postman.com/u/MDrozdz17)\
**Post date:** [August 21, 2019, 4:27pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/3 "2019-08-21T16:27:11Z")

</div>

Thanks for the response sivcan that is very helpful.

On the line below if i wanted to replace “USCM-CLAIMS-TRAINING” with a variable i am using in my GET URL: called {{projectKey}} is there a way to do so? It does not seem to accept {{projectKey}}

response[“jenkinsServer-0”] = “USCM-CLAIMS-TRAINING”;

I was looking at the variables link you sent and it seems that you have to do something like this  
let project = pm.iterationData.get(“projectKey”);

however when i do a console.log on “project” i am getting a null so I am doing something wrong.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 21, 2019, 5:31pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/4 "2019-08-21T17:31:19Z")

</div>

@MDrozdz17 - `pm.iterationData` API is used with the [collection runner](https://learning.getpostman.com/docs/postman/collection_runs/intro_to_collection_runs/), to access the data from the data file per iteration.

To access the globals / environments you can use `pm.environment.get('keyName')` / `pm.globals.get('keyName')` API.

---

<div class="post-metadata">

**Author:** ![MDrozdz17](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mdrozdz17/32/2605_2.png) [@MDrozdz17](https://community.postman.com/u/MDrozdz17)\
**Post date:** [August 21, 2019, 5:48pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/5 "2019-08-21T17:48:56Z")

</div>

@singhsivcan perfect thank you so much.

---

<div class="post-metadata">

**Author:** ![MDrozdz17](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mdrozdz17/32/2605_2.png) [@MDrozdz17](https://community.postman.com/u/MDrozdz17)\
**Post date:** [August 21, 2019, 8:00pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/6 "2019-08-21T20:00:13Z")

</div>

@singhsivcan,

One more question if you don’t mind. I’m not very proficient at Javascript, but i’ve got some data being returned with multiple jobNames as seen below:  
Is there an easy way I can add the jenkinsServer property after each jobName? This one has 3, but some projectKeys have more than 20.

```auto
{
    "jobName-0": "DevOps/job/CFTs/job/secrets-test-mysql/",
    "isTag-0": "false",
    "triggers-0": "push;manual;",
    "token-0": "",
    "buildParameters-0": "VALIDATE_CFT=true\r\nCREATE_CHANGESET=true\r\nEXECUTE_CHANGESET=true\r\nBB_NEW_BRANCH=true\r\nGIT_TREEISH=$BRANCH\r\nBB_SHA=$COMMIT\r\nDEBUG=true\r\nprod_protection=yes;of course",
    "branchRegex-0": "^(bugfix|hotfix|feature|release)/.*",
    "pathRegex-0": "",
    "jobName-1": "DevOps/job/CFTs/job/secrets-test-mysql/",
    "isTag-1": "false",
    "triggers-1": "pullrequest;",
    "token-1": "",
    "buildParameters-1": "VALIDATE_CFT=true\r\nCREATE_CHANGESET=true\r\nEXECUTE_CHANGESET=false\r\nBB_PROPEN=true\r\nBB_PRDEST=$PRDESTINATION\r\nBB_PRID=$PRID\r\nGIT_TREEISH=$BRANCH\r\nBB_SHA=$COMMIT\r\nDEBUG=true\r\nprod_protection=yes;of course",
    "branchRegex-1": "",
    "pathRegex-1": "",
    "jobName-2": "DevOps/job/CFTs/job/secrets-test-mysql/",
    "isTag-2": "false",
    "triggers-2": "prmerged;",
    "token-2": "",
    "buildParameters-2": "VALIDATE_CFT=false\r\nCREATE_CHANGESET=false\r\nEXECUTE_CHANGESET=true\r\nBB_PROPEN=false\r\nBB_PRMERGE=true\r\nBB_PRDEST=$PRDESTINATION\r\nBB_PRID=$PRID\r\nGIT_TREEISH=$PRDESTINATION\r\nDEBUG=true\r\nprod_protection=yes;of course",
    "branchRegex-2": "",
    "pathRegex-2": ""
} 

```

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 22, 2019, 7:04am UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/7 "2019-08-22T07:04:34Z")

</div>

I am assuming you want something like this:

```auto
{
    "jobName-0": "DevOps/job/CFTs/job/secrets-test-mysql/",
    "jenkinsServer-0": "USCM-CLAIMS-TRAINING",
    ...
    "jobName-1": "DevOps/job/CFTs/job/secrets-test-mysql/",
    "jenkinsServer-1": "USCM-CLAIMS-TRAINING",
    ...
    "jobName-2": "DevOps/job/CFTs/job/secrets-test-mysql/",
    "jenkinsServer-2": "USCM-CLAIMS-TRAINING",
} 

```

You can build an array and achieve that.  
The script around that would be:

```auto
// We need the latest lodash, so importing it.
_ = require('lodash');

// Get the response as JSON
let response = pm.response.json();

// Based on the index of the array, we'll assign the value
// Right now, all three values are same but I am assuming
// you might want to change that too, you can either hard code that
// or build a logic around that.

// Number of jobNames
let jobNameCount = _.countBy(_.keys(response), (k) => k.includes('jobName')).true,

  // Fill an array with length of jobNameCount and all the values as 'USCM-CLAIMS-TRAINING' 
  uscms = _.fill(Array(jobNameCount), 'USCM-CLAIMS-TRAINING'),
  // This will be like this -> uscms = ['USCM-CLAIMS-TRAINING', 'USCM-CLAIMS-TRAINING', 'USCM-CLAIMS-TRAINING']

  // We'll populate this object, so declaring it here
  jenkinsServers = {};

_.forEach(uscms, (uscm, index) => {
  jenkinsServers[`jenkinsServer-${index}`] = uscm;
});

// Add all the new jenkins servers to the response object
response = _.assign(response, jenkinsServers);

// Set the whole response to a variable after stringifying it
pm.globals.set('jobResponse', JSON.stringify(response));

```

I haven’t tested the script, but I think it should work.

---

<div class="post-metadata">

**Author:** ![MDrozdz17](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mdrozdz17/32/2605_2.png) [@MDrozdz17](https://community.postman.com/u/MDrozdz17)\
**Post date:** [August 22, 2019, 3:50pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/8 "2019-08-22T15:50:34Z")

</div>

Thank you for all your help! You got me on the right track!

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 22, 2019, 5:03pm UTC](https://community.postman.com/t/add-parameter-to-json-body-put-request-while-retaining-parameters-from-get-request/7310/9 "2019-08-22T17:03:13Z")

</div>

I am glad it helped you out, all the best. 😃
