Make Selenium Easy

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

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

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

  1. What would be the jsonpath for last count ?
    {
    “response”: {
    “homeworksSummary”: [
    {
    “count”: “0”,
    “title”: “Total Homework Created”,
    “icon”: “”,
    “type”: “total”,
    “lastModified”: “”
    },
    {
    “count”: “0”,
    “title”: “Published Homework”,
    “icon”: “”,
    “type”: “published”,
    “lastModified”: “”
    },
    {
    “count”: “2”,
    “title”: “Submited Homework”,
    “icon”: “”,
    “lastModified”: “2021-05-11 17:35:59”,
    “type”: “submitted”
    },
    {
    “count”: “46”,
    “title”: “Homework Saved as Draft”,
    “icon”: “”,
    “lastModified”: “2021-05-11 17:35:59”,
    “type”: “draft”
    }
    ]
    },
    “error”: null,
    “warning”: null
    }

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.