How to save an array of objects?

How to save an array of objects ?
To save to environment variable.

The response is:

{“Status”: 1,
“Message”: “Success”,
“Code”: 0,

"Data": [
    {
        "ID": 100333,
        "Name": "Michael ",
        "Folder": "Folder_1",
        "Is_Available": false
    },
    {
        "ID": 100334,
        "Name": "Lina ",
        "Folder": "Folder_2",
        "Is_Available": false
    },
           {
        "ID": 100335,
        "Name": "Simon ",
        "Folder": "Folder_3",
        "Is_Available": true
    }

]
}

I need to save in the array:

Array_of_objects[0] = “ID”: 100333,
“Name”: "Michael ",
“Folder”: “Folder_1”,
“Is_Available”: false

Array_of_objects[1] = “ID”: 100334,
“Name”: "Lina ",
“Folder”: “Folder_2”,
“Is_Available”: false

Array_of_objects[2] = “ID”: 100335,
“Name”: "Simon ",
“Folder”: “Folder_3”,
“Is_Available”: true
…

Array_of_objects[n] = …

The array length should be unlimited

Thanks!

If I understand your question correctly, you’re trying to save an array to an environment or a variable programmatically.

You can do this within your pre-request or test scripts using postman scripts. The pm object exposes an interface that allows you to set or get new environments of your choice.

Follow this guide to learn more.

1 Like

To store and use the arrays as variables you have to JSON.stringify() them before storing, and JSON.parse() them when you retrieve them.

The array appears to be under the data element. Therefore, the following should allow you to store the array.

pm.environment.set("Data", JSON.stringify(pm.response.json().Data))
1 Like