Postman Variables

Viewed 44

I am trying to parse "Id" from below json to store it in a variable

{
    "cf35864e-944d-11e9-8aff-22000ab8d684": {
        "Id": "1a45b704-944e-11e9-8aff-22000ab8d684",
        "Name": "Test plan",
        "Type": "Limited",
        "Credits": 10119.70,
        "AvailableCredits": 500.100
    }
}

I've tried

bodydata =JSON.parse(responseBody)
planid=bodydata.cf35864e-944d-11e9-8aff-22000ab8d684.Id
console.log(planid)

But postman throws errors, is there any way so that only ID can be fetched.

2 Answers

This should work: planid=bodydata["cf35864e-944d-11e9-8aff-22000ab8d684"]['Id']

As per your mentioned JSON data, the below may work.

let resp = pm.response.json();

console.log(resp.cf35864e-944d-11e9-8aff-22000ab8d684.Id);

Above will work only if "cf35864e-944d-11e9-8aff-22000ab8d684" value is static and only one object is returned.

Related