Master JSONPath in Spring Boot: Extract and Manipulate JSON Like a Pro

This guide shows how to use JSONPath within a Spring Boot 3.4.0 project to efficiently query, filter, and modify complex JSON structures, providing Maven setup, syntax overview, operator tables, and practical code examples for extracting authors, prices, books, and applying custom filters.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master JSONPath in Spring Boot: Extract and Manipulate JSON Like a Pro

Introduction

When dealing with complex JSON structures, manually creating Java POJOs or Maps is cumbersome. JSONPath provides an XPath‑like expression language to query, filter, transform, and update JSON data efficiently.

Environment

Spring Boot 3.4.0 is used. Add the json‑path dependency (version managed by Spring Boot):

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

Syntax Overview

Key operators:

$ – Root element of the query.

@ – Current node processed by a filter predicate.

* – Wildcard for any name or index.

.. – Recursive descent.

.<name> – Dot notation for child nodes.

['<name>'] – Bracket notation for child nodes or collections.

[<number>] – Array index or list of indices.

[start:end] – Array slice.

[?(<expression>)] – Filter expression that must evaluate to a boolean.

Filter Operators

== – Equality (note: 1 ≠ '1').

!= – Inequality.

< – Less than.

<= – Less than or equal.

> – Greater than.

>= – Greater than or equal.

=~ – Regex match.

in – Left value exists in right collection.

nin – Left value does not exist in right collection.

subsetof – Left value is a subset of right collection.

anyof – Intersection exists.

noneof – No intersection.

size – Size of array or string matches right value.

empty – Array or string is empty.

Practical Example

Sample JSON data:

{
  "store": {
    "book": [
      { "category": "Java", "author": "张三", "title": "Java从入门到放弃", "price": 88.6 },
      { "category": "Spring", "author": "Pack", "title": "Spring从入门到精通", "price": 99.8 },
      { "category": "Java", "author": "李四", "title": "多线程并发编程", "isbn": "1-9527-6688-9", "price": 66.9 },
      { "category": "Spring", "author": "Pack", "title": "Spring Boot3实战案例100讲", "isbn": "6-9527-7799-2", "price": 70 }
    ],
    "bicycle": { "color": "red", "price": 666 }
  },
  "expensive": 10
}

Reading the JSON file in Spring Boot:

String json = new ClassPathResource("data.json")
    .getContentAsString(StandardCharsets.UTF_8);

Examples of JSONPath queries:

All authors: $.store.book[*].author All authors using recursive descent: $..author All data under store: $.store.* All prices: $.store..price Third book (index 2): $..book[2] Books with an ISBN: $..book[?(@.isbn)] Books cheaper than 70: $.store.book[?(@.price < 70)] Number of books: $..book.length() Custom filter example (Java code):

Filter cheapFictionFilter = filter(
    where("category").is("Java")
    .and("price").lte(70D)
);
List<Map<String, Object>> books = JsonPath.parse(json)
    .read("$.store.book[?]", cheapFictionFilter);

Modifying JSON data:

String newJson = JsonPath.parse(json)
    .set("$['store']['book'][0]['author']", "pack_xg")
    .jsonString();
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaJSONSpring BootJsonPathData Extraction
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.