#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Rest Assured

Json Path Introduction

What is JsonPath?

JsonPath is a query language (and the Rest Assured class that implements it) for navigating and extracting specific values from a JSON document, similar to how XPath works for XML.

Basic Syntax

JsonPath jp = new JsonPath(responseBody);
String name = jp.getString("data.first_name");
List<String> allIds = jp.getList("data.id");

Why Testers Use JsonPath

JsonPath lets a test navigate deeply nested JSON responses precisely — pulling out one specific field, or a whole list of values, without manually parsing the JSON string.

Practising on a Sample Response

Rest Assured's JsonPath uses Groovy's GPath syntax. Paths start from the root of the document without a leading $, and dots move into nested objects. Take this response from a user list API:

{
  "page": 1,
  "data": [
    { "id": 7, "email": "neha@example.com", "first_name": "Neha", "active": true },
    { "id": 8, "email": "arjun@example.com", "first_name": "Arjun", "active": false },
    { "id": 9, "email": "kiran@example.com", "first_name": "Kiran", "active": true }
  ],
  "support": { "url": "https://example.com/help" }
}

Here is how common questions translate into expressions:

JsonPath jp = response.jsonPath();

int page            = jp.getInt("page");                       // 1
String firstEmail   = jp.getString("data[0].email");           // neha@example.com
String lastName     = jp.getString("data[-1].first_name");     // Kiran
int count           = jp.getInt("data.size()");                // 3
String helpUrl      = jp.getString("support.url");
String arjunEmail   = jp.getString("data.find { it.id == 8 }.email");
List<String> active = jp.getList("data.findAll { it.active }.first_name");  // [Neha, Kiran]

The find and findAll closures are what make GPath powerful: you can filter by any field instead of relying on array positions that may change.

Using Paths Directly in Assertions

You do not always need a separate JsonPath object. The same expressions work inside then().body() with Hamcrest matchers, which keeps simple checks short.

given()
    .baseUri("https://api.example.com")
.when()
    .get("/users?page=1")
.then()
    .statusCode(200)
    .body("data.size()", greaterThan(0))
    .body("data.id", hasItems(7, 8))
    .body("data.find { it.id == 9 }.first_name", equalTo("Kiran"));

Extract values with jsonPath() when you need them later, for example to pass an ID into the next request. Use body() when you only need to verify.

Chaining requests with an extracted value

A typical API test creates something, reads its ID from the response, and then uses that ID in the next call. JsonPath makes this flow short and readable.

int newId = given()
        .contentType("application/json")
        .body("{\"name\": \"Ravi\", \"job\": \"tester\"}")
    .when()
        .post("/users")
    .then()
        .statusCode(201)
        .extract().jsonPath().getInt("id");

given().when().get("/users/" + newId).then().statusCode(200);

If the response is large and you query it many times, create the JsonPath object once and reuse it rather than parsing the body again for every value. You can also call jp.prettify() to print the body neatly while debugging a failing path.

GPath vs Jayway JsonPath Syntax

GoalRest Assured (GPath)Jayway JsonPath
First emaildata[0].email$.data[0].email
All idsdata.id$.data[*].id
Filterdata.findAll { it.active }$.data[?(@.active == true)]

Online JSONPath evaluators usually use the Jayway style, so an expression that works there may fail in Rest Assured. Keep the two syntaxes separate in your head.

Reading Whole Objects, Not Just Fields

When a test needs many fields from the same object, pulling them one by one becomes noisy. getObject maps part of the response straight into a Java class, as long as the field names match. For example, jp.getObject("data[0]", User.class) returns a User with its id, email and name filled in, and jp.getList("data", User.class) returns the whole list. This needs a JSON library such as Jackson or Gson on the classpath, and it keeps assertions focused on meaning rather than paths.

Mistakes Testers Commonly Make

  • Adding $. to the start of a Rest Assured path.
  • Expecting an exception for a wrong path; a missing path often returns null, so assert on it explicitly.
  • Reading numbers with getString and then comparing them as text.
  • Hard-coding array indexes when the order of results is not guaranteed; filter with find instead.
  • Leaving a typo in a field name: JSON keys are case-sensitive.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course