Search code examples
jsonrest-assuredjsonpathrest-assured-jsonpath

How to read nodes in JSONArray using Rest Assured IO JSONPath?


Below is the response of a REST request and my requirement is to verify if name node contains "Military" and amount node equals 500

[
      {
        "incentiveId":11791331,
        "autoApplied":null,
        "name":"Toyota US Lease Loyalty - National excl. Southeast",
        "amount":500,
        "expirationDate":"2018-07-09",
        "minimumTerm":null,
        "maximumTerm":null,
        "groupAffiliation":null,
        "previousOwnership":"Lease Loyalty"
      },
      {
        "incentiveId":11990139,
        "name":"Toyota US Military Appreciation - National excl. Southeast",
        "amount":1000,
        "expirationDate":"2018-12-31",
        "groupAffiliation":"Military",
        "previousOwnership":null
      }
    ]

I am using REST Assured IO API in my project. Could you please help me in coding this requirement?


Solution

  • You have multiple options, but the preferred one are:

    • Map the JSON to PoJo (Java Object) in separate class. Then deserialize the JSON as array of Objects and assert those objects values.

    • Use RestAssured jsonPath method to get to the actual values using jsonPath.

    Hope this helps.