JSON parsing help needed

Hi,

Total noob question here:

How do i get all the “id” values to into “userid”-variable as an array with a test script? Like 1,11,111,1111,11111 etc. At the moment my parser gets all the data within “records” array in the JSON and puts it in the “userid”-variable.

Is have a JSON body such as this:

{

"records": [

    {

        "id": "1",

        "email": "1@1.com"

    },

    {

        "id": "11",

        "email": "11@1.com"

    },

    {

        "id": "111",

        "email": "111@1.com"

    },

    {

        "id": "1111",

        "email": "1111@1.com"

    },

    {

        "id": "11111",

        "email": "11111@1.com"

    },

    {

        "id": "111111",

        "email": "111111@1.com"

    },

    {

        "id": "1111111",

        "email": "1111111@1.com"

    }

]

}

My parser looks like this:

var data = JSON.parse(responseBody);
postman.setEnvironmentVariable(“userid”, JSON.stringify(data.records));
console.log(data.records);

Thank you very much already for your answers! :slight_smile:

You could use something like this to iterate through the data and push each of the id values into the idList array.

The idList array is then saved as a global variable.

idList = []
_.each(pm.response.json().records, (item) => {
    idList.push(item.id)
})

pm.globals.set('idListArray', JSON.stringify(idList))

This is a very basic way of doing what you asked but I’m sure there are much more cleaner solutions.

Thanks Danny! I’ll test your solution for now.

1 Like