How to store a JSON in a variable and use it in the request body?


I want to run a collection with specific test data, but restore the data from before my collection run afterwards.

My idea to achieve this is so far:

  1. Execute a GET request to retrieve the currently available data. Format: JSON
  2. Store that data in an environmentvariable
  3. Run collection that includes overriding the original data with PUT request containing my test data.
  4. Restore the original data from the environmentvariable

However this doesn’t seem to work.

Approach #1:
I used the code from a tutorial to store the data:

let response = pm.response.json();
let savedData = response;
pm.environment.set(“savedData”, savedData );

I checked data saved in the environment variable and it looks fine.

But I cannot pass the data to the body. I tried this in the body:


Approach #2:
I only stored the values of the data, but not the whole JSON:

let response = pm.response.json();
let savedData = response.values;
pm.environment.set(“savedData”, savedData );

And then trying this in the body:

“myData” : [{{“savedData”}}]

Doesn’t work either.

Any idea how to solve this problem?


Try {{savedData}} instead of {{"savedData"}}. :cowboy_hat_face:

Thanks for your answer!

However if I try:
“myData”: [{{savedData}}]

I get the following error message:

Can not deserialize instance of out of START_ARRAY token
at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@39123303; line: 2, column: 24] (through reference chain:[“savedData”]->java.util.ArrayList[0])

Any idea?

@Myxlplyx - So to solve your problem.

  1. In the test script of your GET request, you can put this down:
let response = pm.response.json(),
    savedData = JSON.stringify(response);

pm.environment.set("savedData", savedData);

You need to store the data in stringified format.

  1. In the body of the requests where you want to use this savedData you can simply put it like so:
    Make the body as ‘Raw’ and the type as ‘JSON’:


Put the following as the raw data:

   "myData": [{{savedData}}]

Run the request and you can see the following in the Postman Console


Hi sivcan,
thanks for your answer.

I also found a solution just a few hours ago before I saw your response.

This thread gave me the needed hint:

Here spefically this:

postman.setEnvironmentVariable(“deviceMapping”, responseBody);
//variables need to be strings

After understanding that responseBody stores the whole response body as a string, the solution was as easy as:

  1. In the GET request save the whole JSON response body to an environment variable:

postman.setEnvironmentVariable(“savedData”, responseBody);

  1. To pass the JSON data into a consecutive request just put the following as raw data:


That’s it :slight_smile:

@sivcan, thank you for the clarity here. I couldn’t find this info anywhere else!