REST Assured 58 - What Is JsonPath And How To Create It For Simple And Nested JSON Object?

Keywords: Java JSON API testing JsonPath

REST Assured series summary REST Assured 58 - What Is JsonPath And How To Create It For Simple And Nested JSON Object?

introduce

If you know something about XPath, it's easier to understand JsonPath. The difference is that XPath represents the path to a node in an XML document, while JsonPath represents the path to a node in a JSON document.

In this article, we will learn the following:

  1. What is JsonPath?
  2. JsonPath of simple JSON object
  3. JsonPath of nested JSON object s

prerequisite

The default Rest Assured includes the JsonPath dependency library. Therefore, as long as the Rest Assured dependency library is added, there is no need to add the JsonPath dependency library.

<!-- REST Assured -->
<dependency>
  <groupId>io.rest-assured</groupId>
  <artifactId>rest-assured</artifactId>
  <version>4.4.0</version>
</dependency>

What is JsonPath?

JSON stands for "JavaScript Object Notation". It is a lightweight, language independent, self describing text format. It is a marker for data storage and data exchange. JSON is easier for humans to read and write and is more popular than XML. For more information about JSON, refer to the previous article JSON introduction

A JSON is composed of JSON Nodes or JSON elements. You can traverse a JSON element through path.

For example, a postal address "room 110, unit 11, No. 1111, SHANGDA Road, Baoshan District, Shanghai" can find someone's home according to this address. Similarly, JsonPath is also a path to find and locate a node.

JsonPath of simple JSON object

Simple JSON object

{
  "firstName": "Kevin",
  "lastName": "Zhang"
} 

The above JSON object has two JSON elements or "firstName" and "lastName" nodes enclosed in curly braces. To reach the "firstName" node, we need to start with the first curly bracket. In fact, in any JSON object, there is a root node, represented by "$". Starting from this root node, we can traverse to any node.

Because there is a root node, "firstName" and "lastName" are child nodes. In order to reference a child of a parent node, we use the (.) symbol.

JsonPath of "firstName" node: $. firstName
JsonPath of "lastName" node: $. lastName

You can write the above JsonPath in the Rest Assured script without this symbol ($), because JsonPath has a root node.

In order to use RestAssured JsonPath, you need to get a JsonPath instance of JSON object. In this way, you can get the of JSON elements in different ways.

code:

import io.restassured.path.json.JsonPath;
 
public class SimpleJsonObject {
	
	public static void main(String[] args) {
		
		String jsonString = "{\r\n" + 
				"  \"firstName\": \"Kevin\",\r\n" + 
				"  \"lastName\": \"Zhang\"\r\n" + 
				"}";
		
		//Get JsonPath instance of above JSON string
		JsonPath jsonPath = JsonPath.from(jsonString);
		
		// Since firstName holds a string value use getString() method and provide json path of firstName
		String firstName = jsonPath.getString("firstName");
		String lastName = jsonPath.getString("lastName");
		
		System.out.println("First name is : "+firstName);
		System.out.println("Last name is : "+lastName);
		
		// Since $ is root node of a JSON, we can get whole JSON string using $ as JSON path
		System.out.println(jsonPath.getString("$"));
		// There are two other ways to do the same thing as above
		System.out.println(jsonPath.getString(""));
		System.out.println(jsonPath.get());
		
	}
 
}

Output:

First name is : Kevin
Last name is : Zhang
[firstName:Kevin, lastName:Zhang]
[firstName:Kevin, lastName:Zhang]
{firstName=Kevin, lastName=Zhang}

JsonPath of nested JSON objects

Nested JSON objects are as follows:

{
  "firstName": "Amod",
  "lastName": "Mahajan",
  "address": {
    "houseNo": 404,
    "buildingName": "Not Found",
    "streetName": "Gumnam gali",
    "city": "Bengaluru",
    "state": "Karnataka",
    "country": "India"
  },
  "skills": {
    "language": {
      "name": "Java",
      "proficiency": "Medium"
    }
  }
}

path of "houseNo" node:
root node ($) -> address -> houseNo

Final JsonPath:
$.address.houseNo

Similarly, the JsonPath of the "name" node:
$.skills.language.name

code:

import io.restassured.path.json.JsonPath;
 
public class NestedJsonObject {
	
	public static void main(String[] args) {
		
		String jsonString = "{\r\n" + 
				"  \"firstName\": \"Amod\",\r\n" + 
				"  \"lastName\": \"Mahajan\",\r\n" + 
				"  \"address\": {\r\n" + 
				"    \"houseNo\": 404,\r\n" + 
				"    \"buildingName\": \"Not Found\",\r\n" + 
				"    \"streetName\": \"Gumnam gali\",\r\n" + 
				"    \"city\": \"Bengaluru\",\r\n" + 
				"    \"state\": \"Karnataka\",\r\n" + 
				"    \"country\": \"India\"\r\n" + 
				"  },\r\n" + 
				"  \"skills\": {\r\n" + 
				"    \"language\": {\r\n" + 
				"      \"name\": \"Java\",\r\n" + 
				"      \"proficiency\": \"Medium\"\r\n" + 
				"    }\r\n" + 
				"  }\r\n" + 
				"}";
		
		//Get JsonPath instance of above JSON string
		JsonPath jsonPath = JsonPath.from(jsonString);
		
		// Since houseNo holds an int value use getInt() method and provide json path of houseNo
		int houseNo = jsonPath.getInt("address.houseNo");
		System.out.println("House no is : "+houseNo);
		
		String name = jsonPath.getString("skills.language.name");
		System.out.println("Name is : "+name);
		
	}
 
}

Output:

House no is : 404
Name is : Java

Posted by Zeceer on Sun, 03 Oct 2021 19:48:42 -0700