Day 05: Variables and scripts - Setting the variable in the array - 15 days with Postman

I get stack. Need to set the variable in the array.
Name, email, uuid. To use they in the next request.
( Under the Tests tab of the get Canadian user request, initialize collection variables to store the data returned from the API for the first user in the response object. This information is required for the next request.)

{
“results”: [
{
“gender”: “male”,
“name”: {
“title”: “Mr”,
“first”: “Samuel”,
“last”: “Patel”
},
“location”: {
“street”: {
“number”: 3122,
“name”: “Vimy St”
},
“city”: “Borden”,
“state”: “British Columbia”,
“country”: “Canada”,
“postcode”: “Y0E 7X2”,
“coordinates”: {
“latitude”: “22.6723”,
“longitude”: “-75.7561”
},
“timezone”: {
“offset”: “-6:00”,
“description”: “Central Time (US & Canada), Mexico City”
}
},
“email”: “samuel.patel@example.com”,
“login”: {
“uuid”: “db34f121-9e05-4bb7-a4cf-af3c0125c848”,
“username”: “smallwolf673”,
“password”: “dolemite”,
“salt”: “L5cvRSBA”,
“md5”: “4481421a17f8c0ac6e8edb0f9c78c0e5”,
“sha1”: “5c4f67360c861942fa099951b5b360a5830d4ec4”,
“sha256”: “3413a771c221efd1c00929b596f7951f82b3efa4d61ee17c1cec485c17a1d1e7”
},
“dob”: {
“date”: “1998-01-11T05:42:24.332Z”,
“age”: 25
},
“registered”: {
“date”: “2006-08-23T16:16:47.198Z”,
“age”: 16
},
“phone”: “B01 J75-2546”,
“cell”: “B34 R17-5993”,
“id”: {
“name”: “SIN”,
“value”: “253181994”
},
“picture”: {
“large”: “https://randomuser.me/api/portraits/men/77.jpg”,
“medium”: “https://randomuser.me/api/portraits/med/men/77.jpg”,
“thumbnail”: “https://randomuser.me/api/portraits/thumb/men/77.jpg
},
“nat”: “CA”
}
],
“info”: {
“seed”: “ee379f59e2e1f802”,
“results”: 1,
“page”: 1,
“version”: “1.4”
}
}

I tried
var jsonData = JSON.parse(responseBody);
console.log(jsonData[0].name.first);
pm.environment.set(“Name”, jsonData[0].name.first);

In console I see TypeError: Cannot read properties of undefined (reading ‘first’)
Help me to set Name, email, uuid. To use they in the next request.

Use the console log to show the elements you are trying to target.

When you get an undefined response, it means it can’t find the element.

At which point you should go back to the first element, and target the structure one element at a time.

This should show you that the first element in the response is called results.

console.log(pm.response.json().results[0].name.first);

That’s also the old way of parsing the response. (See my example of the new way using the pm.response function).

Thanks it works. Could you give me a hint were I can read more about arrays?

Postman uses JavaScript under the hood, so any JavaScript course should suffice.

W3Schools is a good place to start.

1 Like

When you do look this up, try to understand the nuances between JSON and JavaScript objects.

Usually, you will parse the response using the following code or similar.

const response = pm.response.json() 

This is parsing the JSON response into a JavaScript object, so at this point forward you are working with a JavaScript object.

There are some JSON Syntax Rules that sort of apply to JavaScript objects, but aren’t exactly the same.

Data is in name/value pairs
Data is separated by commas
Curly braces hold objects
Square brackets hold arrays

What is JSON (w3schools.com)
JavaScript Objects (w3schools.com)

So first thing to look for in a response is those curly braces and square brackets. If an array only has one element, you still have to target it with the the array position [0].

Some JavaScript commands like map() only work against arrays. (Which is a useful command for extracting certain elements from an array).

Also have a look at the following.

JSON Vs. JavaScript: What is the Difference? (koombea.com)

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.