Q:

retrieve and access stringified data

I have Get request which gives me response like below
{
"var1": "value1",
"var2": "value2"
}

I am saving it in an environment variable from Tests script as below
postman.setEnvironmentVariable("allData", JSON.stringify(responseBody));

In next Post request, I am trying to retrieve above values from Pre-request script as below
var jsonData = JSON.parse(allData)

However I am getting not defined error as below

There was an error in evaluating the Pre-request Script: 
  ReferenceError: allData is not defined

I can set each property in an individual variable and that works fine but that pollutes environment (as there are around 20 such properties).  Please suggest better alternate for the same.  Also suggest me how to access individual values in Body of the Post request.  Can I do something like below?
{
  "var1": "{{jsonData.var1}}",
  "var2": "{{jsonData.var2}}"
}

OR I need to set values to individual variable in Pre-request script and use them in Body?
Thanks

A:

If need to retrieve the data from the saved variable as a whole data set, you would need to make this reference to it when declaring the variable:    
var jsonData = JSON.parse(pm.environment.get("allData"))

If you want to be able to use the single values from your variable in the Request Body you would need to parse them individually, in the Pre-Request Script, then store them as variables to use in the Request Body:
pm.environment.set("my_single_var_1", JSON.parse(pm.environment.get('allData')).var1)

pm.environment.set("my_single_var_2", JSON.parse(pm.environment.get('allData')).var2)

From here you can then set the {{my_single_var_1}} syntax in the Request Body and these placeholders would resolve to the values you have set.

