0

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.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Stephen
  • 13
  • 5

2 Answers2

0

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

let res = pm.response.json();
pm.environment.set("elementId", Object.values(res.value)[0]);
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
0

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.values(x.value)[0])
a0m0rajab
  • 115
  • 7