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

@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’:

image

Put the following as the raw data:

{
   "myData": [{{savedData}}]
}

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

Edit: FYI you can now try that yourself by forking the following collection :arrow_heading_down:
https://www.postman.com/postman/workspace/postman-answers/collection/9215231-47464531-0c3f-4b21-94b8-71e48025f88f?ctx=documentation

7 Likes