6

I have a test in postman and the response comes back with 'nested' data. By that I mean we have a 'data' section of the response and a 'messages' section. Inside data there are a ton of other fields and those are the ones I need to be verifying on with Jetpacks. How can I get to these fields?

this is what the json response looks like:

{
  "Data": {
    "QRCode_ID": 168,
    "Repairer_ID": null,
    "AssignedToEmployee_ID": null,
    "TaskName": "003021919913",
    "DueDate": "2015-07-02T00:12:53.597",
    "DueDateTimeSpan": 1959471956224,
    "TaskStatus_ID": 1,
    "Description": "due 6/30, 5:00",
    "TaskUrgency_ID": null,
    "TaskType_ID": null,
    "DueDateDisplay": "2015-07-02 00:12",.......
      }
  },
  "Messages": [
    "success"
  ]
}

And this is what my postman test looks like:

var data = JSON.parse(responseBody);
tests["Verify QRCode_ID is correct"] = data.QRCode_ID === 168;
Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43
besaidAuroch
  • 61
  • 1
  • 6

1 Answers1

8

You can test for nested data much in the same way you test for data that is not nested (using dot notation)

I created a really quick dummy service that returns the the following json:

{
  "one": "1",
  "two": "2",
  "three": {
    "four": "4",
    "five": "5"
  }
}

In the following snippet I test (using dot notation) values in the nested object. In particular I'm asserting that the object three has properties of four and five that are set to the values of "4" and "5" respectively:

var data = JSON.parse(responseBody);
tests["4"] = data.three.four === "4";
tests["5"] = data.three.five === "5";

Here's my setup in postman with the corresponding json response:enter image description here

Here are my test results: enter image description here

GargantuanTezMaximus
  • 1,044
  • 1
  • 12
  • 19
  • 1
    thank you! and while i was on that track i figured out how to access stuff in arrays... i.e. tests["Verify task id"] = data.Data.Items[0].taskId === 715; thanks everyone so much!! – besaidAuroch Jul 03 '15 at 14:30
  • 1
    If my answer helped you can you please accept it as the correct answer by clicking the check mark next to it? – GargantuanTezMaximus Jul 09 '15 at 01:57