Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
This article will teach you how to create a JSONpath in REST assured. We will learn that for simple JSON objects in REST assured, nested JSON objects in REST assured and JSON arrays.
As we know, testing in Java is more challenging than in languages like Groovy or Ruby. This is because these dynamic languages use fewer lines of code for writing functions, but we still use Java because it is faster than these languages. Java also provides more stability and comparatively fewer errors.
REST stands for REpresentation of State Transfer. And REST assured is the Java library. It validates the response of your code in the dynamic languages to the Java domain.
JSONpath
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 in REST assured. It is a query language for JSON.
You must learn how to write a perfect JSONpath expression to retrieve complex data.
JSONpath In REST Assured
There exists JSONpath in REST assured dependency by default. There are a lot of JSONpath expressions. Only a few are supported by Rest assured. To learn more, read JSONpath expressions in REST assured.
JSONpath for Simple and Nested JSON Objects
What are JSON objects? JSON objects contain key-value pairs.
Id, name, and email are called keys. The answers to these keys are called values. A key-value pair is separated by a colon. The collection of key-value pairs is stored in JSON objects. It is surrounded by curly brackets {}.
JSONpath for Simple JSON Objects in REST ASSURED
Simple JSON objectsin REST assured are the kinds we discussed above. Simple JSON objects in REST assured always have a root node. It is the starting point of a JSON. Every object in a root node of simple JSON objects in REST assured is called a child node.
To represent a root node for simple JSON objects in REST assured. We use the “ $ “ symbol. And to represent the child node, we use a dot “. ”
For example:
{
“Name” : “Anchita”
“Last name” : “sharma”
}
JSONpath for this simple JSON objects in REST assured will be :
For name node - $.name
For last name node - $.lastname
You don’t need to write the $ symbol for the root node in REST assured. As it already considers root nodes to exist in a JSONpath. You need to insert a JSON object instance under JSONpath in REST assured.
For example:
INPUT
import io.restassured.path.JSON.JSONPath;
public class JSONpathforrestassured{
public static void main(String[] args) {
String JSONstring ="{\r\n" +
" \"Name\": \"Anchita\",\r\n" +
" \"lastName\": \"sharma\"\r\n" +
"}";
JSONPath JSONPath = JSONPath.from(JSONstring);
String Name = JSONPath.getString("Name");
String lastName = JSONPath.getString("lastName");
System.out.println("First name is : "+Name);
System.out.println("Last name is : "+lastName);
}
}
You can also try this code with Online Java Compiler
To write JSONpath for nested JSON objects in REST assured: $ + .child node + . child node
To write JSONpath for nested JSON objects in REST assured, for attribute “email” in the above example. you will write: $.address.email
You don’t need to write the $ symbol for the root node in REST assured. As it already considers root nodes to exist in a JSONpath. You need to insert a JSON object instance under JSONpath.
JSONpath for Simple and Nested JSON Arrays
Similar types of JSON objects are stored in JSON arrays.
For example:
The JSON objects 0, 1, 2, 3, and 4 contain the same key pair format. And are stored in a JSON array. It is represented by straight brackets [].
JSONpath for Simple JSON Arrays
Simple JSON arrays consist of multiple JSON objects separated by commas. For example:
In programming languages, indexes are used to refer to and access elements. The same applies to JSONpath. In the above example, if we write address[0]. It will represent the first element in that array. And so on.
To access children's elements, those are elements under an array. We use dot “ . ” as we learned in JSONpath for simple and nested JSON objectsin REST assured.
For example, let’s create a program for the above-given array.
A nested JSON array is a collection of JSON arrays. Thus a nested JSON array consists of arrays of nested JSON objects. We can write JSONpath for nested JSON arrays same as JSONpath for simple JSON arrays, but that's a topic on its own.
JSON can be hijacked and is not completely free from viruses either. The attacker targets a system with access to JSON data. And then can retrieve data from a JSON file.
Which is better, postman or rest assured?
Rest assured is better than the postman. Rest assured, you can reuse code as it is a Java client. Moreover, in postman, we can provide only one data file for each collection, unlike rest assured.
Is rest assured a BDD framework?
Yes. Rest assured is a java library used for testing. It is a behavior-driven-development framework. Such as when and then notations.
How to convert JSON to string?
JSON.stringify() is used for this purpose. The result will be a string. It will follow the JSON notation.
Why is JSON used over XML?
JSON is designed for data interchange. Thus it is faster than XML. Moreover, JSON can be parsed by a javascript function. Whereas XML requires an XML parser.