-3

I have a nested JSON returned from an API that I am hitting using a GET request, in POSTMAN chrome app. My JSON looks like this.

{
"resultset": {
    "violations": {
        "hpd": [
            {
                "0": {
                    "ViolationID": "110971",
                    "BuildingID": "775548",
                    "RegistrationID": "500590",
                    "Boro": "STATEN ISLAND",
                    "HouseNumber": "275",
                    "LowHouseNumber": "275",
                    "HighHouseNumber": "275",
                    "StreetName": "RICHMOND AVENUE",
                    "StreetCode": "44750",
                    "Zip": "10302",
                    "Apartment": "",
                    "Story": "All Stories ",
                    "Block": "1036",
                    "Lot": "1",
                    "Class": "A",
                    "InspectionDate": "1997-04-11",
                    "OriginalCertifyByDate": "1997-08-15",
                    "OriginalCorrectByDate": "1997-08-08",
                    "NewCertifyByDate": "",
                    "NewCorrectByDate": "",
                    "CertifiedDate": "",
                    "OrderNumber": "772",
                    "NOVID": "3370",
                    "NOVDescription": "§ 27-2098 ADM CODE FILE WITH THIS DEPARTMENT A REGISTRATION STATEMENT FOR BUILDING. ",
                    "NOVIssuedDate": "1997-04-22",
                    "CurrentStatus": "VIOLATION CLOSED",
                    "CurrentStatusDate": "2015-03-10"
                },
                "count": "1"
            }
        ]
    }
},
"count": "1",
"total_page": 1,
"current_page": 1,
"limit": [
    "0",
    "1000"
],
"status": "success",
"error_code": "",
"message": ""
}

I am trying to test whether my response body has "ViolationID":"110971".

I tried the below code in postman:

var jsonData =JSON.parse(responseBody);
tests["Getting Violation Id"] = jsonData.resultset.violations.hpd[0].ViolationID === 110971;
Aaroninus
  • 1,062
  • 2
  • 17
  • 35
Kapil Rana
  • 1
  • 1
  • 1
  • Does this answer your question? [Checking a value in a nested JSON using Postman](https://stackoverflow.com/questions/42850233/checking-a-value-in-a-nested-json-using-postman) – Henke Jan 24 '21 at 08:33

2 Answers2

0

Two issues I noticed in the provided data. The following suggestions might help you:

  1. Add missing closing braces at the end.
  2. Add missing 0 in the index like this: resultset.violations.hpd[0].0.ViolationID
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
nams
  • 1
0

If the hpd array always contains only 1 member, the test might be pretty straightforward:

pm.test('Body contains ViolationID', () => {
    const jsonBody = pm.response.json();
    const violationId = jsonBody.resultset.violations.hpd[0]["0"].ViolationID;
    pm.expect(parseInt(violationId)).to.eql(110971);
})

However, if hpd array might contain more than one member, it gets a bit trickier. I would suggest mapping only ViolationID keys from nested objects:

pm.test('Body contains ViolationID', () => {
    const jsonBody = pm.response.json();
    const violationIds = jsonBody.resultset.violations.hpd.map(hpd => hpd["0"].ViolationID);
    pm.expect(violationIds).to.contain('110971');
})
n-verbitsky
  • 552
  • 2
  • 9
  • 20