1

Refer screenshot:

enter image description here

{
    "urn:pearson:work:876fa82c-9ad7-49c3-80c9-02d3b4c84312": {
        "id": "urn:pearson:work:876fa82c-9ad7-49c3-80c9-02d3b4c84312",
        "type": "element-authoredtext",
        "schema": "http://schemas.pearson.com/wip-authoring/element/1",
        "elementdata": {

This is not working:

var jsonData = pm.response.json();
pm.environment.set("id", jsonData.id);
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Sunny Goel
  • 11
  • 2
  • 1
    This might help: https://stackoverflow.com/questions/1116708/getting-first-json-property – g_bor Jan 14 '21 at 14:33

1 Answers1

1
var jsonData = pm.response.json();
pm.environment.set("id", jsonData["urn:pearson:work:876fa82c-9ad7-49c3-80c9-02d3b4c84312"].id);

JsonData will have the full response , Id is inside "urn:pearson:work:876fa82c-9ad7-49c3-80c9-02d3b4c84312" field.

As it has special character you should call it as json[key] , then the next key which is id could be called as json.id, as it doesn't have any special characters

Update

for dynamic value use:

var jsonData = pm.response.json();
pm.environment.set("id", jsonData[Object.keys(jsonData)[0]].id);

The above will select the first property , but if you want to make sure it is the right one then use array.find():

let jsonData = pm.response.json();
let prop = Object.keys(jsonData)
prop = prop.find(element => element.includes("urn:pearson:work"))
pm.environment.set("id", jsonData[prop].id);
PDHide
  • 18,113
  • 2
  • 31
  • 46