# Postman test - How to manipulate environment variable JSON values

**URL:** <https://community.postman.com/t/postman-test-how-to-manipulate-environment-variable-json-values/20995>\
**Category:** Help Hub\
**Tags:** variables, json, collections, newman-project, tests\
**Created:** [March 4, 2021, 7:28am UTC](https://community.postman.com/t/postman-test-how-to-manipulate-environment-variable-json-values/20995 "2021-03-04T07:28:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjayaram](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rjayaram](https://community.postman.com/u/rjayaram)\
**Post date:** [March 4, 2021, 7:28am UTC](https://community.postman.com/t/postman-test-how-to-manipulate-environment-variable-json-values/20995/1 "2021-03-04T07:28:18Z")

</div>

Here is my requirement: Appreciate any pointers:

1. Make a post request - Done
2. Save the portion of the JSON response into env variable (var name = envbody) - Done
3. Read the env variable (envbody) to get JSON schema with values - Done
4. Read csv file has 100 rows with query and locale - Done
5. envbody environment variable has Json object stringified - Done
6. envbody has json with one of attribute name called query.  
Replace query attribute value of envbody with csv iteration data query value
7. Repeat till end of csv file

I have done 1-5. How do I manipulate / insert value into envbody variable  
Here is the code snippet

```auto

 var resultObject = JSON.parse(responseBody);
 var searchBody = resultObject.searchPostBody;
 var searchBodyStr= JSON.stringify(searchBody);
 postman.setEnvironmentVariable("searchPostBody", searchBodyStr);

```

Reading csv file - No issues

Need to replace one of the attribute value in searchPostBody env variable using csv iteration data

For example, in this case, `searchPostBody` has a Json object with one of the attribute names is `query (q)`.

I need to replace the value of “q” with the value which is read from the csv file.

```auto
{
    "expe": "xxx",
    "q": "",
    "limit": 10,
    "locale": "en_US",
    "timeout": 10000
}

```

Thanks, Raje

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 4, 2021, 12:14pm UTC](https://community.postman.com/t/postman-test-how-to-manipulate-environment-variable-json-values/20995/2 "2021-03-04T12:14:01Z")

</div>

Hey @rjayaram

If I had this value saved as an environment variable:

```auto
{
    "expe": "xxx",
    "q": "",
    "limit": 10,
    "locale": "en_US",
    "timeout": 10000
}

```

I would change the `q` value using `pm.iterationData.get('query')` by adding this to the `Pre-request Script`:

```auto
let searchPostBody = JSON.parse(pm.environment.get('searchPostBody'))

searchPostBody.q = pm.iterationData.get('query');

pm.environment.set('searchPostBody', JSON.stringify(searchPostBody))

```

The set the `Request Body` to:

```auto
{{searchPostBody}}

```

* * *

In the Runner, I would select my data file (I’ve used a JSON file here):

```auto
[
	{
		"query": "one"
	},
	{
		"query": "two"
	},
	{
		"query": "three"
	},	
	{
		"query": "four"
	},	
	{
		"query": "five"
	}
]

```

When this is run, it will grab the environment variable value, set the `q` property to the `iterationData` value before storing that back into the environment variable again. The POST request will be sent with the new value in the `q` property.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/b/db7bc4c2c178a22ef2309e8fad574131f1ada58e.png)

* * *

---

<div class="post-metadata">

**Author:** ![rjayaram](https://avatars.discourse-cdn.com/v4/letter/r/3d9bf3/32.png) [@rjayaram](https://community.postman.com/u/rjayaram)\
**Post date:** [March 4, 2021, 11:18pm UTC](https://community.postman.com/t/postman-test-how-to-manipulate-environment-variable-json-values/20995/3 "2021-03-04T23:18:32Z")

</div>

Hi @danny-dainton

Thanks for your suggestion. It worked perfectly.

Appreciate your help.

Thaks, Raje
