Extract Values from JSON response in Rest Assured

Until now we are sending requests to the server and we are receiving the responses. But in actual scenarios, we are not going to end test cases just by sending and receiving the responses; once we receive the responsible have to pass the response to verify certain field values or use it for some other purpose.

So the response could be in different formats as of this particular topic will see how to parse the JSON response.

There are two ways to parse a JSON response with respect to rest assured:

  • JayWay Jsonpath
  • JsonPath Rest Assured ( based on groovy)

JayWay Jsonpath in Rest Assured

Location: https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path/

GitHub Page : https://github.com/json-path/JsonPath

<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->

Go through the documentation present in the GitHub page for a complete tutorial. Here we are going to see how to perform a basic extraction using that Jayway JsonPath.

The endpoint we are going to use: https://chercher.tech/sample/api/product/read?id=3793

The above-given endpoint id may or may not be present when you are trying to use this particular and point. So please make sure that the ID is present when you are trying this particular example, If it is not present then try to use some Other ID.

Operators/Locators in Jayway JsonPath:

To play with below operators, please visit the JSONPath evaluator: https://jsonpath.herokuapp.com/

Operator DescriptionDescription
$ Fetches the complete response body
@ To look for a specific attribute
* Wildcard. Available anywhere a name or numeric is required.
.. Fetches value for the given property; the given property could be anywhere inside simple/complex JSON
.<name> Looks for a specific attribute throughout the response; can be used in combination as well
['<name>' (, '<name>')] Bracket-notated child or children
[<number> (, <number>)] Array index or indexes
[start:end] Array slice operator; must be used the only o arrays
[?(<expression>)] Filter expression. The expression must evaluate to a boolean value.
String json = " json response from server";
List<String> result = JsonPath.read(json, "JsonPath Locator");

A simple example of parsing the JSON response using JSONPath with rest assured.

void testLogging() {
	String endpoint = "https://chercher.tech/sample/api/product/read?id=3793";
	String json = given().when().get(endpoint).asString();
	System.out.println("-----Complete json---");
	// fetch complete response
	String completeResponse = JsonPath.read(json, "$").toString();
	// fetch first array
	String firstArrayItem = JsonPath.read(json, "$[0]").toString();
	// fetch all the id attribute values
	String allIDs = JsonPath.read(json, "$..id").toString();
	// we can use $[0]id as well; similarly we can write for other attributes
	String firstID = JsonPath.read(json, "$[0].id").toString();


Complex JSON example;

    "store": {
        "book": [
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99

As of the moment, Our API does not support nested JSON responses, so we will be storing the above-given JSON as a string and we will be using that string for the JSONPath.

void testLogging() {
	String json = "paste above json here";
	String allAuthors = JsonPath.read(json, ".author").toString();
	// select book element only if it has category attribute
	String selectBasedOnCriteria = JsonPath.read(json, "$..book[?(@.category)]").toString();


About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions