Make Selenium Easy

REST Assured Tutorial 58 – What is JsonPath and how to create it for simple and nested JSON Object?

Last updated on December 23rd, 2020 at 02:32 pm

Introduction

As a part of the End to End REST Assured Tutorial, in this post, we will learn about JsonPath.

If you are familiar with Xpath then you can understand JsonPath easily. The only difference is that XPath represents a path to reach a node in an XML document while JsonPath represents a path to reach a node in a JSON document.

Topics we are going to learn in this post

  1. What is JsonPath?
  2. How to write JsonPath for a simple JSON object?
  3. How to write JsonPath for a nested JSON object?

Prerequisite

As we are using RestAssured which includes JsonPath dependency by default, there is no need to include JsonPath dependency explicitly. I have used the below version of Rest Assured for this post:-

What is JsonPath?

JSON stands for “JavaScript Object Notation” which is a lightweight, language-independent, and self-describing format in-text for data storing and interchange. JSON is easy to read, write and create for humans which makes it famous over XML. To know more about JSON refer to my previous post – Introduction To JSON

A JSON consists of JSON Nodes or JSON elements. To traverse to a JSON element there will be a path. For example- Consider the below postal address:-

Actually, the above address shows a path to reach there. You need to go to India -> Karnataka -> Bengaluru -> Gumnma Gali -> No More apartment -> #404.

Similarly, a JsonPath represents a path to reach a JSON element and extract its value.

How to write JsonPath for a simple JSON object?

There are different syntaxes to write JsonPath but let’s start with a very simple JSON Object.

In the above JSON Object, we have two JSON elements or nodes “firstName” and “lastName” enclosed in two curly braces. To reach node “firstName” we need to start from the top curly brace. In fact, in any JSON object, there will be a root node which is represented as “$”(dollar). From this root node, we need to start to traverse through a JSON element.

Since there is a root node and “firstName” and “lastName” are children (direct) or child nodes of the root node. To reference a child node of a parent node we need to use the dot (.) symbol.

JsonPath for node “firstName” – $.firstName

JsonPath for node “lastName” – $.lastName

Whenever we write above JsonPath in RestAssured scripts, we don’t include dollar($) sign as it expects JsonPath has a root node.

To use JsonPath with RestAssured example, we need to get JsonPath instance of JSON Object, and then we can use respective methods to get the value of JSON element.

Example program

Output

How to write JsonPath for a nested JSON object?

Let’s take the below example where I have nested JSON elements. JSON element “houseNo” is child node of “address”. JSON element “name” is a child node of “language” which is a child node of “skills”. This type of JSON object is called nested JSON object.

If we want to write JSON path for node “houseNo” then we need to traverse as below:-

root node ($) -> address -> houseNo

Since we represent a child of a parent using dot (.) the final JSONPath will be:-

$.address.houseNo

Similarly, for node “name”, JSON Path will be:-

$.skills.language.name

Example Program

Output

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 58 – What is JsonPath and how to create it for simple and nested JSON Object?

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: