Table of contents
1.
Introduction 
2.
JSONpath
2.1.
JSONpath In REST Assured
3.
JSONpath for Simple and Nested JSON Objects
3.1.
JSONpath for Simple JSON Objects in REST ASSURED
3.2.
JSONpath for Nested JSON Objects in REST ASSURED
4.
JSONpath for Simple and Nested JSON Arrays
4.1.
JSONpath for Simple JSON Arrays
4.2.
JSONpath for Nested JSON Array
5.
Frequently Asked Questions
5.1.
Is JSON secure or free from viruses?
5.2.
Which is better, postman or rest assured?
5.3.
Is rest assured a BDD framework?
5.4.
How to convert JSON to string?
5.5.
Why is JSON used over XML?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

REST Assured – What is JSONpath and how to create it for simple and nested JSON Objects?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 Assured – What is JSONpath and how to create it for simple and nested JSON Objects?

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.

JSON logo

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.  

For example:

 {
	“Id” : 2
	“Name” : “Anchita sharma”
	“Emai” l: “anchita.sharma@codingninjas.com” 
 }


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 objects in 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 : 

  1. For name node  - $.name
  2. 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
Run Code


OUTPUT

output image

As discussed above, we have inserted simple JSON objects in REST assured instance under JSONpath.

JSONpath for Nested JSON Objects in REST ASSURED

Nested JSON objects in REST assured group JSON objects in layers. To increase clarity in case of large key-value pairs.

For example:

 {
	“Id “ : “1”
	“Name” : “Anchita”
	“Address” :	
	{
		“Phone number”: “831*******”
		“home” : “street 21 near CR , mumbai”
		“Email” : “anchita.sharma@codingninjas.com”
	}
	“Education” : 	
	{
		“School” : “delhi public school mumbai”
		“College: st.xaviers mumbai”
	}
 }


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:

JSON array 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:

{
  "Name": "Anchita",
  "lastName": “sharma",
  "address": [
    {
      "type": "temporary",
      "city": "mumbai",
      "state": "maharashtra"
    },
    {
      "type": "permanent",
      "city": "Ajmer",
      "state": “Rajasthan"
    }
  ]
}


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 objects in REST assured.

For example, let’s create a program for the above-given array.

INPUT

import java.util.List;
import java.util.Map;
 
import io.restassured.path.json.JsonPath;
 
public class SimpleJsonArray {
 
	public static void main(String[] args) {

		String jsonArrayString = "{\r\n" +
									"  \"Name\": \"Anchita\",\r\n" +
									"  \"lastName\": \"Sharma\",\r\n" +
									"  \"address\": [\r\n" +
									"    {\r\n" +
									"      \"type\": \"temporary\",\r\n" +
									"      \"city\": \"mumbai\",\r\n" +
									"      \"state\": \"maharashtra\"\r\n" +
									"    },\r\n" +
									"    {\r\n" +
									"      \"type\": \"permanent\",\r\n" +
									"      \"city\": \"Ajmer\",\r\n" +
									"      \"state\": \"Rajasthan\"\r\n" +
									"    }\r\n" +
									"  ]\r\n" +
									"}";
		JSONPath JSONPath = JSONPath.from(JSONstring);
		String addressType1 = JSONPath.getString("address[0].type");
		System.out.println("Address type is : "+addressType1);

		String addressType2 = JSONPath.getString("address[1].type");
		System.out.println("Another address type is : "+addressType2);
	}
}
You can also try this code with Online Java Compiler
Run Code


OUTPUT 

output image

JSONpath for Nested JSON 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.

Must Read Array of Objects in Java

Now let's discuss some FAQs

Frequently Asked Questions

Is JSON secure or free from viruses?

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.

Conclusion

In this article, we discussed various basics of REST assured. We learned how to create JSONpath for simple and nested JSON objects in REST assured. And how to create JSONpath for simple and nested JSON arrays.
To learn more about REST assured, refer to the Building RequestSpecification Using RequestSpecBuilderFetching Values From JSON objects in rest assured, and creating JSON objects and arrays.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass