Looping through an array with an index value

i’ve been searching for an answer but I’m hitting the wall on this one. I have a json array

let jsonData = xml2Json(pm.response.text());

This is a valid json Object, and I can work with it. Here’s the json itself (or a snippet)

{
“users”: {
“count”: “740”,
“pageNumber”: “1”,
“pageSize”: “500”,
“user”: [
{
“authType”: “1”,
“createDate”: “May 14, 2018 9:56:33 PM”
}
]
}
}

using the lodash function ._each, I should be able to access users.user[0].authType

For some reason it is coming back as undefined. The json has approximately 2,000 entries, so I’m trying to figure out should I just a traditional for loop and abandon ._each? or is there a way to include a generic variable for the index of ._each?

P.

Hello @pr85 Welcome to the Community :bouquet:

Are you trying to parse the XML response? If not why are you using

let jsonData = xml2Json(pm.response.text());

I suggest you to use below snippet if you are using the SOAP/XML response. Also the syntax for reaching the element varies for SOAP.

var jsonObject = xml2Json(responseBody);
console.log(jsonObject);

Can you please provide more screenshots/details here, like what you are trying to do exactly?

But I tried your JSON response to reach authType,

var resp = JSON.parse(responseBody);

console.log(resp.users.user[0].authType);

its working. So please provide more clarity here to help you better :blush:

I suggest you to read the following article for using basic loadash functions in Postman.

It’s not about getting the response back, that’s fine. It’s more about looping through more than one response.

so for example

_.each(jsonData.results, (result) => {
pm.globals.set('authType, result.users.user.authType)
})

This doesn’t seem to work as expected.

P.

Hi @pr85,

Welcome to the community! :clap:

For clarity, you say ‘looping through more than one response’. Are you performing this function for one Postman request, or multiple Postman requests? If so, this code will have to be specific to each request you have, in each individual test script section.

If you can provide more context, I can provide more help.

Best,
Orest