How to use pm.response.text() to pull a certain value from array

I have been using

var jsonData = JSON.parse(responseBody);
theExistingUserID = jsonData[0][“id”];

new code looks like:

var jsonData = JSON.parse(pm.response.text());
theExistingID = jsonData[“id”];

but with responseBody being depricated, I’m trying to update my code to use 'pm.response.text(). However when I use that I’m getting errors that the value is ‘undefinded’

response text looks like:

{id: 48737, title: “For Payments, determine completion rates both on PCI Remodel-specific work”, description: “For Payments, determine completion rates both on PCI Remodel-specific work”…}

Any thoughts, help would be greatly appreciated.

Hey @jka4cu :wave:,

Welcome to the Postman Community :postman:

You should be able to just use pm.response.json() as a straight swap out for JSON.parse(responseBody).

that does work, but I can’t parse for the value “id” afterwards … I can see the whole body parsed.

Given your example response, it should be like this:

let jsonData = pm.response.json();
let theExistingID = jsonData.id;
console.log(theExistingID);

I would highly recommend utilising the Postman Console to help to see what is returned in those variables.

I figured it out

let theExistingID = null;

var jsonData = pm.response.json();
theExistingID = jsonData[“id”];

that give me the results I needed.

I think there are a lot of contextual details and maybe the full response body missing from the question.

If for your example, the body was like this:

{
    "id": 48737,
    "title": "For Payments, determine completion rates both on PCI Remodel-specific work",
    "description": "For Payments, determine completion rates both on PCI Remodel-specific work"
}

A post-response script of this would have extracted that:

let jsonData = pm.response.json();
let theExistingID = jsonData.id;
console.log(theExistingID);

I’m not really sure how this is fitting in here? I can only assume that there are details missing :thinking:

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