Stripping characters from response

Hi,

A newbie question -

the response I get is in the following form:
[
{
“unitnumber”: “255272”,
“name”: “unit-272”,
“unittype”: 7,
…
}
]

How do I extract the json response that is enclosed in the [ brackets ] ?

Thanks

Hi @eyalasko,

You don’t need to strip the characters as such - they are a valid part of the response.

The brackets indicate that the response is an array of items. If you want to get at the properties within, you need to first declare the index of the item that you wish to select. (The index will start from 0, so the first item is always zero)

For example, this will print “255272” to the Postman console:

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

If you’re certain that your response will only ever contain one item, you can save yourself some typing by specifying the index in the first variable, for example:

var firstItem = pm.response.json()[0];
console.log(firstItem.unitnumber);
console.log(firstItem.name);
1 Like