skip to Main Content

I have a response as below:

{
    "value": {
        "element-6066-11e4-a52e-4f735466cecf": "b721a18e-ffab-49bc-acdf-5a30c84e160d"
    }
}

I want to extract the value of they dynamic key labelled "element-6066-xxxx-xxxx-xxxxxxxxx" (changes each time I re-run the API call).

Any guidance would be greatly appreciated.

I tried the following:

var data = JSON.parse(responseBody);
pm.environment.set("elementId", Object.keys(data.value)[0]);

But this sets the "key" instead of the "value" (expecting ‘b721a18e-ffab-49bc-acdf-5a30c84e160d’ to be set.

2

Answers


  1. You would need to use Object.values() for this:

    let res = pm.response.json();
    pm.environment.set("elementId", Object.values(res.value)[0]);
    
    Login or Signup to reply.
  2. The Object.values() would help you. you can refer to this MDN docs. Here is an example of that:

    let x = {
        "value": {
            "element-6066-11e4-a52e-4f735466cecf": "b721a18e-ffab-49bc-acdf-5a30c84e160d"
        }
    }
    
    console.log(Object.keys(x.value)[0])
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search