Parse Json to get value in collectionVariables

Hi there,

I am trying to understand how to parse json with Postman.
My goal is to retrieve the value of WorkstationId and store it into collectionVariables.
Could some one point me in the right direction?

This is how far I got but it failed:

pm.collectionVariables.set("GETWorkstationIdStore", pm.response.json().value[0].Workstations.WorkstationId);

let GETWorkstationId = pm.variables.get("GETWorkstationIdStore");
console.log("WorkstationId from GET = " + GETWorkstationId)

My JSON file

  "@odata.context": "https://doesnotexist/MyApi/api/v1.0/$metadata#locations",
  "value": [
    {
      "LocationIdInternal": 1,
      "LocationId": "B9507A00-9057-4CCC-A66B-9AAAB1B6CA5B",
      "DisplayName": "DEFAULT_LOCATION",
      "IsActive": true,
      "Workstations": [
        {
          "WorkstationIdInternal": 3,
          "WorkstationId": "8E5B3291-E092-4091-8C9A-58B7C90E907C",
          "WorkstationName": "OVER77",
          "WorkstationType": "Default"
        }
      ]
    }
  ]
}

Hey @Phyxius

You should be able to do just this in the Tests for that request:

let GETWorkstationId = pm.response.json().value[0].Workstations[0].WorkstationId

console.log("WorkstationId from GET = " + GETWorkstationId)

pm.collectionVariables.set("GETWorkstationIdStore", GETWorkstationId);

Perfect, thank you very much.
Much appreciated

Do you know of any document where I can readup on how to build parse JSON like you did:
.value[0].Workstations[0].WorkstationId

No worries. Happy to help.

Not sure what you mean? Do you mean in terms of looping through those arrays rather than hard coding the index numbers?

Both actually.
I am starting to learn.
So I can image that there is a tutorial of some sort that will explain to me how to build parameter for pm.responds.json.

for instance my line was
pm.response.json().value[0].Workstations.WorkstationId
btu it shoudl have been
pm.response.json().value[0].Workstations[0].WorkstationId

difference in the line is apparent
I would like to know why and when to apply those parameters.

Also looping through arrays sounds something I can use.
Would like to know more. :slight_smile: