Home > database >  Set Postman variable based on a json without tag
Set Postman variable based on a json without tag

Time:09-22

I'm pretty new with using postman and I'm trying to store one part of the JSON response of my request in a variable. My problem is that the JSON doesn't have tags. It looks like this:

[
    "01000511",
    "00000001",
    "00000009",
    "01000654"
]

Is it possible to store the JSON as an array and create variables like this: id1=response[0], id2=response[1]? Thanks in advance!

CodePudding user response:

Assuming the response is just like you described, you can do this using the "Tests" tab in your Postman request.

Postman tests tab

Here is the code

// If the response is exactly like you described (You may have to adapt the query to get your array)
const resp = pm.response.json()

for (var i = 0;i<resp.length;i  ) {
    pm.environment.set("id" i, resp[i]);
}

This result like this in your Env catalog:

Env results

  • Related