0

I am running a chain of collections from 1 API call to another. I want to pull the response body from API call 1 to API call 2 so that it can post the data to my application.

I have created the environment variables and in the tests tab I have created a script to set the variables but when I run the script I get a response of the below:

SyntaxError: Unexpected token [

My test script is:

bodyData = JSON.parse(responseBody)

value = bodyData.[1]country

console.log(value)

The response body looks like this

[
    {
        "Country": "United Kingdom",
    }
]

I know the issue is [] and needs to have a string before it but the API is not defined with a string and I cant just use the below as it's then undefined:

bodyData = JSON.parse(responseBody)

value = bodyData.country

console.log(value)

Any idea how I can get this to work?

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
FreshX
  • 57
  • 2
  • 10
  • Does this answer your question? [setting multiple variables in Postman JSON test scrip](https://stackoverflow.com/questions/61540416/setting-multiple-variables-in-postman-json-test-scrip) – Henke Feb 28 '21 at 06:48

1 Answers1

0

You have the reference slightly wrong I think.

This should do it:

let bodyData = pm.response.json()

let country = bodyData[0].Country

console.log(country) 

To then set the variable you would need to use:

pm.environment.set("country", country)
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • how can i set multiple variables in the above i.e let bodyData = pm.response.json() let country = bodyData[0].Country let postcode= bodyData[0].postcode console.log(country,postcode) pm.environment.set("country","postcode", country, postcode) – FreshX May 01 '20 at 09:13
  • You would need to do that individually and set each as it's own variable. There are ways to get all that data into a single object and save it as a variable, then grab the data you need in future requests. If you raise another question with more of the details I can help you with that. – Danny Dainton May 01 '20 at 09:26
  • Hi Danny, posted under https://stackoverflow.com/questions/61540416/setting-multiple-variables-in-postman-json-test-scrip – FreshX May 01 '20 at 10:06