Introduction
This article will teach you how to write robust tests in JSONPath expressions in REST assured. We will also learn how to deserialize JSONpath expressions in REST assured.
As we know, testing in Java is more challenging than in languages like Groovy or Ruby. These dynamic languages use fewer lines of code for writing functions. We still use Java because it is faster than these languages. It also provides more stability and usually fewer errors comparatively.
REST stands for REpresentational State Transfer. And Rest assured is the Java library. It validates the response of your code in the dynamic languages to the Java domain. You can do this in three simple steps:
-
Create a request with parameters.
-
Send the request through the HTTP method.
- Evaluate the response to validate exciting things.
JSONpath Expression
Javascript Object Notation or JSON is a format for storing data.
The query language is the language the user uses to request the database for some data or information.
To retrieve data from a JSON document, we use JSONpath. It is a query language for JSON.
You must learn how to write a perfect JSONpath expression (or syntax) in REST assured to retrieve complex data.
There are a lot of JSONpath expressions in REST assured. Only a few are supported by Rest assured.
We will learn about all the JSONpath expressions that Rest assured supports.
REST assured uses Groovy Gpath notation of JSONpath expressions in REST assured.
JSONPath Evaluator
Suppose we have a GET query consisting of JSON elements. To retrieve the information, you need to write a JSONpath expression in REST assured.
We will take additional support from the JSONpath evaluator.
Steps guide:
-
Paste the GET query in the input.
-
Write a JSONpath expression in REST assured in the “JSON path” box. You can do that in the following ways.
A. $ symbol - gives all the details from the root node.
B. $ + “ . ” + array name - gives data in that array.
C. $ + [‘array name’] - gives data in that array.
D. $ + [‘array name’] + [*] + .string name - gives all the string values in that array.
E. $ + [‘array name’] + [index number] - gives values in that index of the array.
- Evaluation results will show the retrieved data.
Writing JSONPath Expressions
Before writing the JSONpath expression in REST assured, you must write a few lines of code to add a JSON Object to the JSONpath for retrieving data.
The code will include the following:
String JSONObject = “{<JSON Object> }”;
\\ Converting JSONobject to JSONpath expression.
JSONPath JSONPath = JSONPath.from(JSONObject);
To include the data, import the JSONpath header file. Inside the curly brackets of “string JSONobject = { }”.
Now you can write JSONPath expressions.
As discussed above, only a few JSONPath expressions are supported by REST assured.
That is as follows:
JSONpath.getstring:
-
JSONpath.getstring(“ [index number].variablename”): This returns the value in that index with the string name as specified.
-
JSONpath.getstring(“size()”): This returns the size of the array.
-
JSONpath.getstring(“find{it.variablename}”): This finds and then returns its value.
JSONpath.getlist:
-
JSONpath.getlist(“variablename”): This will pick variable names from all elements and return the values.
-
JSONpath.getlist(“findAll{it.variablename}”): This finds and then returns its value.
JSONpath.getJSONObject:
-
JSONpath.getJasonObject(“[index number]”): This returns all values in the element of that index.
For example:
We will write a JSONpath expression for the following JSON array.
[{
"id": 1,
"firstname": "Anchita",
"lastname": "Sharma",
"email": "anchita.sharma@codingninjas.com",
"gender": "female"
}, {
"id": 2,
"firstname": "Anshit",
"lastname": "Singh",
"email": "anshitsingh@codingninjas.com",
"gender": "Male"
}, {
"id": 3,
"firstname": "Stuti",
"lastname": "Jain",
"email": "stutijain@codingninjas.com",
"gender": "Female"
}]
INPUT:
package JsonPathSyntax;
import java.io.File;
import io.restassured.path.json.JsonPath;
public class SyntaxForJsonArray {
public static void main(String[] args) {
String JSONarray=”{[{
"id": 1,
"firstname": "Anchita",
"lastname": "Sharma",
"email": "anchita.sharma@codingninjas.com",
"gender": "female"
}, {
"id": 2,
"firstname": "Anshit",
"lastname": "Singh",
"email": "anshitsingh@codingninjas.com",
"gender": "Male"
}, {
"id": 3,
"firstname": "Stuti",
"lastname": "Jain",
"email": "stutijain@codingninjas.com",
"gender": "Female"
}]}”
JsonPath jsonPath = JsonPath.from(JSONarray);
// To get one value using index
System.out.println("First name of the first employee :"+ jsonPath.getString("[0].firstname"));
// To get whole indexed element
System.out.println("All details of first employee : " +jsonPath.getJsonObject("[0]"));
// To get one value from different JSON objects in the array
System.out.println("First name of all employees :" + jsonPath.getList("firstname"));
// To get the list as of specific group
System.out.println("First name of all female employees : "+jsonPath.getList("findAll{it.gender == 'Female'}.firstname"));
// We can get size of array using size()
System.out.println("Total number of employees : " + jsonPath.getString("size()"));
}
}
OUTPUT:
