eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

One of the advantages of XML is the availability of processing — including XPath — which is defined as a W3C standard. For JSON, a similar tool called JSONPath has emerged.

This tutorial will give an introduction to Jayway JsonPath, a Java implementation of the JSONPath specification. It describes setup, syntax, common APIs and a demonstration of use cases.

Further reading:

Integration Testing in Spring

A quick guide to writing integration tests for a Spring Web application.

The HttpMediaTypeNotAcceptableException in Spring MVC

Learn how to deal with the HttpMediaTypeNotAcceptableException in Spring.

Binary Data Formats in a Spring REST API

In this article we explore how to configure Spring REST mechanism to utilize binary data formats which we illustrate with Kryo. Moreover we show how to support multiple data formats with Google Protocol buffers.

2. Setup

To use JsonPath, we simply need to include a dependency in the Maven pom:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.9.0</version>
</dependency>

3. Syntax

We’ll use the following JSON structure to demonstrate the syntax and APIs of JsonPath:

{
    "tool": 
    {
        "jsonpath": 
        {
            "creator": 
            {
                "name": "Jayway Inc.",
                "location": 
                [
                    "Malmo",
                    "San Francisco",
                    "Helsingborg"
                ]
            }
        }
    },

    "book": 
    [
        {
            "title": "Beginning JSON",
            "price": 49.99
        },

        {
            "title": "JSON at Work",
            "price": 29.99
        }
    ]
}

3.1. Notation

JsonPath uses special notation to represent nodes and their connections to adjacent nodes in a JsonPath path. There are two styles of notation: dot and bracket.

Both of the following paths refer to the same node from the above JSON document, which is the third element within the location field of creator node, that is a child of the jsonpath object belonging to tool under the root node.

First, we’ll see the path with dot notation:

$.tool.jsonpath.creator.location[2]

Now let’s look at bracket notation:

$['tool']['jsonpath']['creator']['location'][2]

The dollar sign ($) represents root member object.

3.2. Operators

We have several helpful operators in JsonPath:

  • Root node ($) denotes the root member of a JSON structure whether it is an object or array. We included usage examples in the previous subsection.
  • Current node (@) represents the node being processed. We mostly use it as part of input expressions for predicates. Suppose we are dealing with book array in the above JSON document; the expression book[?(@.price == 49.99)] refers to the first book in that array.
  • Wildcard (*) expresses all elements within the specified scope. For instance, book[*] indicates all nodes inside a book array.

3.3. Functions and Filters

JsonPath also has functions that we can use at the end of a path to synthesize that path’s output expressions: min(), max(), avg(), stddev() and length().

Finally, we have filters. These are boolean expressions to restrict returned lists of nodes to only those that calling methods need.

A few examples are equality (==), regular expression matching (=~), inclusion (in) and check for emptiness (empty). We mainly use filters for predicates.

For a full list and detailed explanations of different operators, functions and filters, please refer to the JsonPath GitHub project.

4. Operations

Before we get into operations, a quick sidenote: This section makes use of the JSON example structure we defined earlier.

4.1. Access to Documents

JsonPath has a convenient way to access JSON documents. We do this through static read APIs:

<T> T JsonPath.read(String jsonString, String jsonPath, Predicate... filters);

The read APIs can work with static fluent APIs to provide more flexibility:

<T> T JsonPath.parse(String jsonString).read(String jsonPath, Predicate... filters);

We can use other overloaded variants of read for different types of JSON sources, including Object, InputStream, URL and File.

To make things simple, the test for this part does not include predicates in the parameter list (empty varargs). But we’ll discuss predicates in later subsections.

Let’s start by defining two sample paths to work on:

String jsonpathCreatorNamePath = "$['tool']['jsonpath']['creator']['name']";
String jsonpathCreatorLocationPath = "$['tool']['jsonpath']['creator']['location'][*]";

Next, we will create a DocumentContext object by parsing the given JSON source jsonDataSourceString. The newly created object will then be used to read content using the paths defined above:

DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString);
String jsonpathCreatorName = jsonContext.read(jsonpathCreatorNamePath);
List<String> jsonpathCreatorLocation = jsonContext.read(jsonpathCreatorLocationPath);

The first read API returns a String containing the name of the JsonPath creator, while the second returns a list of its addresses.

And we’ll use the JUnit Assert API to confirm the methods work as expected:

assertEquals("Jayway Inc.", jsonpathCreatorName);
assertThat(jsonpathCreatorLocation.toString(), containsString("Malmo"));
assertThat(jsonpathCreatorLocation.toString(), containsString("San Francisco"));
assertThat(jsonpathCreatorLocation.toString(), containsString("Helsingborg"));

4.2. Predicates

Now that we have the basics, let’s define a new JSON example to work on and illustrate how to create and use predicates:

{
    "book": 
    [
        {
            "title": "Beginning JSON",
            "author": "Ben Smith",
            "price": 49.99
        },

        {
            "title": "JSON at Work",
            "author": "Tom Marrs",
            "price": 29.99
        },

        {
            "title": "Learn JSON in a DAY",
            "author": "Acodemy",
            "price": 8.99
        },

        {
            "title": "JSON: Questions and Answers",
            "author": "George Duckett",
            "price": 6.00
        }
    ],

    "price range": 
    {
        "cheap": 10.00,
        "medium": 20.00
    }
}

Predicates determine true or false input values for filters to narrow down returned lists to only matched objects or arrays. We can easily integrate a Predicate into a Filter by using it as an argument for its static factory method. The requested content can then be read out of a JSON string using that Filter:

Filter expensiveFilter = Filter.filter(Criteria.where("price").gt(20.00));
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
  .read("$['book'][?]", expensiveFilter);
predicateUsageAssertionHelper(expensive);

We could also define our customized Predicate and use it as an argument for the read API:

Predicate expensivePredicate = new Predicate() {
    public boolean apply(PredicateContext context) {
        String value = context.item(Map.class).get("price").toString();
        return Float.valueOf(value) > 20.00;
    }
};
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
  .read("$['book'][?]", expensivePredicate);
predicateUsageAssertionHelper(expensive);

Finally, a predicate can be directly applied to read API without the creation of any objects, which is called an inline predicate:

List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
  .read("$['book'][?(@['price'] > $['price range']['medium'])]");
predicateUsageAssertionHelper(expensive);

All three of the Predicate examples above are verified with the help of the following assertion helper method:

private void predicateUsageAssertionHelper(List<?> predicate) {
    assertThat(predicate.toString(), containsString("Beginning JSON"));
    assertThat(predicate.toString(), containsString("JSON at Work"));
    assertThat(predicate.toString(), not(containsString("Learn JSON in a DAY")));
    assertThat(predicate.toString(), not(containsString("JSON: Questions and Answers")));
}

5. Configuration

5.1. Options

Jayway JsonPath provides several options to tweak the default configuration:

  • Option.AS_PATH_LIST returns paths of the evaluation hits instead of their values.
  • Option.DEFAULT_PATH_LEAF_TO_NULL returns null for missing leaves.
  • Option.ALWAYS_RETURN_LIST returns a list even when the path is definite.
  • Option.SUPPRESS_EXCEPTIONS makes sure no exceptions are propagated from path evaluation.
  • Option.REQUIRE_PROPERTIES requires properties defined in the path when an indefinite path is evaluated.

Here is how to apply Option from scratch:

Configuration configuration = Configuration.builder().options(Option.<OPTION>).build();

and how to add it to an existing configuration:

Configuration newConfiguration = configuration.addOptions(Option.<OPTION>);

5.2. SPIs

JsonPath’s default configuration with the help of Option should be enough for the majority of tasks. However, users with more complex use cases can modify the behavior of JsonPath according to their specific requirements — using three different SPIs:

  • JsonProvider SPI lets us change the ways JsonPath parses and handles JSON documents.
  • MappingProvider SPI allows for customization of bindings between node values and returned object types.
  • CacheProvider SPI adjusts the manners that paths are cached, which can help to increase performance.

6. Example Use Cases

We now have a good understanding of the JsonPath functionality. So, let’s look at an example.

This section illustrates dealing with JSON data returned from a web service.

Assume we have a movie information service that returns the following structure:

[
    {
        "id": 1,
        "title": "Casino Royale",
        "director": "Martin Campbell",
        "starring": 
        [
            "Daniel Craig",
            "Eva Green"
        ],
        "desc": "Twenty-first James Bond movie",
        "release date": 1163466000000,
        "box office": 594275385
    },

    {
        "id": 2,
        "title": "Quantum of Solace",
        "director": "Marc Forster",
        "starring": 
        [
            "Daniel Craig",
            "Olga Kurylenko"
        ],
        "desc": "Twenty-second James Bond movie",
        "release date": 1225242000000,
        "box office": 591692078
    },

    {
        "id": 3,
        "title": "Skyfall",
        "director": "Sam Mendes",
        "starring": 
        [
            "Daniel Craig",
            "Naomie Harris"
        ],
        "desc": "Twenty-third James Bond movie",
        "release date": 1350954000000,
        "box office": 1110526981
    },

    {
        "id": 4,
        "title": "Spectre",
        "director": "Sam Mendes",
        "starring": 
        [
            "Daniel Craig",
            "Lea Seydoux"
        ],
        "desc": "Twenty-fourth James Bond movie",
        "release date": 1445821200000,
        "box office": 879376275
    }
]

where the value of release date field is milliseconds since the Epoch, and box office is revenue of a movie in the cinema in US dollars.

We are going to handle five different working scenarios related to GET requests, supposing that the above JSON hierarchy has been extracted and stored in a String variable named jsonString.

6.1. Getting Object Data Given IDs

In this use case, a client requests detailed information on a specific movie by providing the server with the movie’s exact id. This example demonstrates how the server looks for requested data before returning to the client.

Say we need to find a record with id equaling to 2.

The first step is to pick up the correct data object:

Object dataObject = JsonPath.parse(jsonString).read("$[?(@.id == 2)]");
String dataString = dataObject.toString();

The JUnit Assert API confirms the existence of several fields:

assertThat(dataString, containsString("2"));
assertThat(dataString, containsString("Quantum of Solace"));
assertThat(dataString, containsString("Twenty-second James Bond movie"));

6.2. Getting the Movie Title Given Starring

Let’s say we want to look for a movie starring an actress called Eva Green. The server needs to return title of the movie that includes Eva Green in the starring array.

The succeeding test will illustrate how to do that and validate the returned result:

@Test
public void givenStarring_whenRequestingMovieTitle_thenSucceed() {
    List<Map<String, Object>> dataList = JsonPath.parse(jsonString)
      .read("$[?('Eva Green' in @['starring'])]");
    String title = (String) dataList.get(0).get("title");

    assertEquals("Casino Royale", title);
}

6.3. Calculation of the Total Revenue

This scenario makes use of a JsonPath function called length() to figure out the number of movie records in order to calculate the total revenue of all the movies.

Let’s look at the implementation and testing:

@Test
public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() {
    DocumentContext context = JsonPath.parse(jsonString);
    int length = context.read("$.length()");
    long revenue = 0;
    for (int i = 0; i < length; i++) {
        revenue += context.read("$[" + i + "]['box office']", Long.class);
    }

    assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue);
}

6.4. Highest Revenue Movie

This use case exemplifies the usage of a non-default JsonPath configuration option, namely Option.AS_PATH_LIST, to find out the movie with the highest revenue.

First, we need to extract a list of all the movies’ box office revenue. Then we convert it to an array for sorting:

DocumentContext context = JsonPath.parse(jsonString);
List<Object> revenueList = context.read("$[*]['box office']");
Integer[] revenueArray = revenueList.toArray(new Integer[0]);
Arrays.sort(revenueArray);

We can easily pick up the highestRevenue variable from the revenueArray sorted array and then use it for working out the path to the movie record with the highest revenue:

int highestRevenue = revenueArray[revenueArray.length - 1];
Configuration pathConfiguration = 
  Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> pathList = JsonPath.using(pathConfiguration).parse(jsonString)
  .read("$[?(@['box office'] == " + highestRevenue + ")]");

Based on that calculated path, we’ll determine and return the title of the corresponding movie:

Map<String, String> dataRecord = context.read(pathList.get(0));
String title = dataRecord.get("title");

The whole process is verified by the Assert API:

assertEquals("Skyfall", title);

6.5. Latest Movie of a Director

This example will illustrate how to figure out the last movie directed by a director named Sam Mendes.

To begin with, we create a list of all the movies directed by Sam Mendes:

DocumentContext context = JsonPath.parse(jsonString);
List<Map<String, Object>> samMendesMovies = context.read("$[?(@.director == 'Sam Mendes')]");

We can also define a Filter on the root node director and use it as a predicate:

Filter directorSamMendesFilter = Filter.filter(Criteria.where("director")
    .contains("Sam Mendes"));
List<Map<String, Object>> samMendesMovies = JsonPath.parse(jsonString)
    .read("$[?]", directorSamMendesFilter);

We then use that list for the extraction of release dates. Those dates will be stored in an array and then sorted:

List<Object> dateList = new ArrayList<>();
for (Map<String, Object> item : samMendesMovies) {
    Object date = item.get("release date");
    dateList.add(date);
}
Long[] dateArray = dateList.toArray(new Long[0]);
Arrays.sort(dateArray);

We use the lastestTime variable (the last element of the sorted array) in combination with the director field’s value to determine the title of the requested movie:

long latestTime = dateArray[dateArray.length - 1];
List<Map<String, Object>> finalDataList = context.read("$[?(@['director'] 
  == 'Sam Mendes' && @['release date'] == " + latestTime + ")]");
String title = (String) finalDataList.get(0).get("title");

The following assertion proves that everything works as expected:

assertEquals("Spectre", title);

7. Conclusion

This article covered fundamental features of Jayway JsonPath — a powerful tool to traverse and parse JSON documents.

Although JsonPath has some drawbacks, such as a lack of operators for reaching parent or sibling nodes, it can be highly useful in a lot of scenarios.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)