I see the get request result but can't output a key value to console; says undefined

I am trying to troubleshoot an environment variable problem. I am trying to see if postman can recognize the value, but when i refer to it after getting the results back, it keeps saying undefined in console.

Successful get request body:
{
“id”: 35438,
“user_id”: 35418,
“account_id”: 1,
“unique_id”: “d006”,
}

Testing to see if console sees the “user_id value” so I can store it in a variable later:

This displays the full response successfully:
var jsonData = pm.response.json();
console.log(jsonData);

console log:
(1) [{…}]
:arrow_forward:0: {…}
id: 35438
user_id: 35418
account_id: 1
unique_id: “d006”

However, if I do as below, it says “undefined”, instead of giving me “35418”

var jsonData = pm.response.json();
console.log(jsonData.user_id);

I tried to do
var jsonData = JSON.parse(responseBody);
console.log(jsonData.user_id);

I am getting the same result. What am I doing wrong?
Thanks.

@tulgae Welcome to the community :partying_face:

I see both the methods are correct here. It does the same job,

var jsonData = pm.response.json();
console.log(jsonData.user_id);

or

var jsonData = JSON.parse(responseBody);
console.log(jsonData.user_id);

But if you see the response in the console, it is as array.

So please try,

var jsonData = pm.response.json();
console.log(jsonData[0].user_id);

To understand more about the path, https://jsonpathfinder.com/ is useful. And I have written a small blog about it here.

In future, this will help yon to correct “undefined” error in Postman.

I hope this is helpful. Please let us know if you still facing an issue here :blush:

1 Like

Thank you! It worked.

1 Like

@tulgae Good to know! All the best :raised_hands:

1 Like