Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

463
Views
RestAssured: get key value from json object with specified value

In a test case I perform a call with the following response:

[
    {
        "ourID": "770050010000000010",
        "date": "2019-03-07",
        "otherValue": null
    },
    {
        "ourID": "770050010000000020",
        "date": "2019-03-07",
        "otherValue": null
    }
]

The test is performed by Serenity and I use RestAssured to perform the call:

Response response = RestAssuredApiHelper.baseRestCall(headers, baseUrl, path)
                .body(requestBody)
                .post();

assertThat(response.getBody().jsonPath().get("$.[?(@.ourID=='770050010000000010')].date"), contains("2019-03-07"));

The assertion is not working, probably because RestAssured uses Gpath instead of JsonPath, but in all the docs i've been reading there are examples of ways this could work. What am I missing here?

The error I get is:

java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: [ @ line 1, column 29.
                            $.[?(@.meteringPointEANID=='770050010000000010')].energySource
                               ^

1 error
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

check with below code:--(i believe that json expression is correct , did not check)

Object dateObject = JsonPath.parse(response.getBody().asString()).read("$.[?(@.ourID=='770050010000000010')].date");
String dateString = dataObject.toString();
assertThat(dateString, containsString("2019-03-07"));

POM Dependency:---

<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.1.0</version>
</dependency>
over 4 years ago · Santiago Trujillo Report

0

An alternative of using JsonPath with RestAssured is to use the build-in deserialization mechanism of RestAssured.

If you don't know what the return type of the object is you could check this out: https://github.com/rest-assured/rest-assured/wiki/usage#deserialization-with-generics

The generic solution would look something like this:

Response response = RestAssuredApiHelper.baseRestCall(headers, baseUrl, path)
                .body(requestBody)
                .post();

List<Map<String, Object>> objects =  response.getBody().as(new TypeRef<>() {});
Map<String, Object> singleObject = objects.stream().filter((Map<String, Object> object) -> object.get("ourID").equals("770050010000000010"))
                .peek(System.out::println)
                .findFirst().get();

System.out.println(singleObject.get("ourID")); // 770050010000000010

If you know what the response object is it, it is even simpeler:

List<YourResponseObject> yourResponseObjects = response.getBody().as(new TypeRef<>() {});
over 4 years ago · Santiago Trujillo Report

0

assertEquals(response.path("find {it.ourID == '770050010000000010'}.date"), "2019-03-07")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!