Java JsonPath API found at jayway JsonPath might have changed a little since all the above answers/comments. Documentation too. Just follow the above link and read that README.md, it contains some very clear usage documentation IMO.

Basically, as of current latest version 2.2.0 of the library, there are a few different ways of achieving what's been requested here, such as:

Pattern:
--------
String json = "{...your JSON here...}";
String jsonPathExpression = "$...your jsonPath expression here..."; 
J requestedClass = JsonPath.parse(json).read(jsonPathExpression, YouRequestedClass.class);

Example:
--------
// For better readability:  {"store": { "books": [ {"author": "Stephen King", "title": "IT"}, {"author": "Agatha Christie", "title": "The ABC Murders"} ] } }
String json = "{\"store\": { \"books\": [ {\"author\": \"Stephen King\", \"title\": \"IT\"}, {\"author\": \"Agatha Christie\", \"title\": \"The ABC Murders\"} ] } }";
String jsonPathExpression = "$.store.books[?(@.title=='IT')]"; 
JsonNode jsonNode = JsonPath.parse(json).read(jsonPathExpression, JsonNode.class);

And for reference, calling 'JsonPath.parse(..)' will return an object of class 'JsonContent' implementing some interfaces such as 'ReadContext', which contains several different 'read(..)' operations, such as the one demonstrated above:

/**
 * Reads the given path from this context
 *
 * @param path path to apply
 * @param type    expected return type (will try to map)
 * @param <T>
 * @return result
 */
<T> T read(JsonPath path, Class<T> type);

Hope this help anyone.

Answer from franky duke on Stack Overflow
🌐
GitHub
github.com › json-path › JsonPath
GitHub - json-path/JsonPath: Java JsonPath implementation · GitHub
A Java DSL for reading JSON documents. Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.
Starred by 9.4K users
Forked by 1.7K users
Languages   Java
🌐
Baeldung
baeldung.com › home › json › introduction to jsonpath
Introduction to JsonPath | Baeldung
November 13, 2023 - An introduction to Jayway JsonPath, a Java implementation of the JSONPath specification, including setup, syntax, common APIs, and use cases.
Top answer
1 of 5
14

Java JsonPath API found at jayway JsonPath might have changed a little since all the above answers/comments. Documentation too. Just follow the above link and read that README.md, it contains some very clear usage documentation IMO.

Basically, as of current latest version 2.2.0 of the library, there are a few different ways of achieving what's been requested here, such as:

Pattern:
--------
String json = "{...your JSON here...}";
String jsonPathExpression = "$...your jsonPath expression here..."; 
J requestedClass = JsonPath.parse(json).read(jsonPathExpression, YouRequestedClass.class);

Example:
--------
// For better readability:  {"store": { "books": [ {"author": "Stephen King", "title": "IT"}, {"author": "Agatha Christie", "title": "The ABC Murders"} ] } }
String json = "{\"store\": { \"books\": [ {\"author\": \"Stephen King\", \"title\": \"IT\"}, {\"author\": \"Agatha Christie\", \"title\": \"The ABC Murders\"} ] } }";
String jsonPathExpression = "$.store.books[?(@.title=='IT')]"; 
JsonNode jsonNode = JsonPath.parse(json).read(jsonPathExpression, JsonNode.class);

And for reference, calling 'JsonPath.parse(..)' will return an object of class 'JsonContent' implementing some interfaces such as 'ReadContext', which contains several different 'read(..)' operations, such as the one demonstrated above:

/**
 * Reads the given path from this context
 *
 * @param path path to apply
 * @param type    expected return type (will try to map)
 * @param <T>
 * @return result
 */
<T> T read(JsonPath path, Class<T> type);

Hope this help anyone.

2 of 5
11

There definitely exists a way to query Json and get Json back using JsonPath. See example below:

 String jsonString = "{\"delivery_codes\": [{\"postal_code\": {\"district\": \"Ghaziabad\", \"pin\": 201001, \"pre_paid\": \"Y\", \"cash\": \"Y\", \"pickup\": \"Y\", \"repl\": \"N\", \"cod\": \"Y\", \"is_oda\": \"N\", \"sort_code\": \"GB\", \"state_code\": \"UP\"}}]}";
 String jsonExp = "$.delivery_codes";
 JsonNode pincodes = JsonPath.read(jsonExp, jsonString, JsonNode.class);
 System.out.println("pincodesJson : "+pincodes);

The output of the above will be inner Json.

[{"postal_code":{"district":"Ghaziabad","pin":201001,"pre_paid":"Y","cash":"Y","pickup":"Y","repl":"N","cod":"Y","is_oda":"N","sort_code":"GB","state_code":"UP"}}]

Now each individual name/value pairs can be parsed by iterating the List (JsonNode) we got above.

for(int i = 0; i< pincodes.size();i++){
    JsonNode node = pincodes.get(i);
    String pin = JsonPath.read("$.postal_code.pin", node, String.class);
    String district = JsonPath.read("$.postal_code.district", node, String.class);
    System.out.println("pin :: " + pin + " district :: " + district );
}

The output will be:

pin :: 201001 district :: Ghaziabad

Depending upon the Json you are trying to parse, you can decide whether to fetch a List or just a single String/Long value.

Hope it helps in solving your problem.

🌐
Javadoc.io
javadoc.io › doc › com.jayway.jsonpath › json-path › 2.2.0 › com › jayway › jsonpath › JsonPath.html
JsonPath - json-path 2.2.0 javadoc
Latest version of com.jayway.jsonpath:json-path · https://javadoc.io/doc/com.jayway.jsonpath/json-path · Current version 2.2.0 · https://javadoc.io/doc/com.jayway.jsonpath/json-path/2.2.0 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/com.jayway.j...
🌐
OpenRewrite
docs.openrewrite.org › reference › jsonpath and jsonpathmatcher reference
JsonPath and JsonPathMatcher reference | OpenRewrite Docs
JsonPathMatcher provides methods for matching the given cursor location using a provided JsonPath expression. "JsonPath" (sometimes stylized as JSONPath) is a query language for JSON, similar to how XPath is a query language for XML.
Published   2 weeks ago
🌐
RestfulAPI
restfulapi.net › home › json › json with jsonpath
JSON with JSONPath - REST API Tutorial
November 4, 2023 - JSONPath is a query language for JSON. JSONPath is used for selecting and extracting a sub-section from the JSON document.
🌐
GitHub
github.com › json-path › JsonPath › blob › master › json-path › src › main › java › com › jayway › jsonpath › JsonPath.java
JsonPath/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java at master · json-path/JsonPath
* JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is · * available in many programming languages such as Javascript, Python and PHP.
Author   json-path
🌐
Medium
medium.com › @vishubommoju › jsonpath-library-underrated-library-that-can-handle-the-complex-nested-jsons-easily-in-java-f1503c7c3c4f
JsonPath library ,underrated library that can handle the complex nested JSONs easily in JAVA | by Vishu Bommoju | Medium
September 23, 2020 - JsonPath library ,underrated library that can handle the complex nested JSONs easily in JAVA JsonPath is one of the underrated library in the java to handle the JSON . JsonPath can be used in most of …
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java libraries › extracting json using jsonpath with examples
Extracting JSON using JsonPath with Examples
August 22, 2022 - Learn to extract information from JSON documents using Jayway’s JsonPath. JsonPath is similar to Xpath for XML documents.
🌐
TOOLSQA
toolsqa.com › rest-assured › jsonpath-and-query-json-using-jsonpath
What is JSONPath and How to query JSON using JSONPath?
JSONPath creates a uniform standard and syntax to define different parts of a JSON document. JSONPath defines expressions to traverse through a JSON document to reach to a subset of the JSON.
🌐
Medium
medium.com › @khileshsahu2007 › jsonata-jsonpath-and-jmespath-exploring-capabilities-and-limitations-bf491348022d
JSONata, JSONPath, and JMESPath: Exploring Capabilities and Limitations | by Khilesh Sahu | Medium
December 19, 2024 - For JSON manipulation and querying, three notable libraries stand out: JSONata, JSONPath, and JMESPath. These tools offer powerful capabilities for extracting and transforming JSON data, each with its unique strengths and limitations. While this article focuses on their capabilities from a Java ...
🌐
Java Tips
javatips.net › api › com.jayway.jsonpath.jsonpath
Java Examples for com.jayway.jsonpath.JsonPath - Javatips.net
@Test public void testJsonPath() throws UnRAVLException { // drives configuration UnRAVLRuntime r = new UnRAVLRuntime(); assertNotNull(r); String document = "{ \"s\": \"string\", \"b\": true, \"i\": 100, \"n\": 0.5, \"o\": { \"x\": 0, \"y\" : 0 }, \"a\": [ 0,1,2,3,4,5] }"; JsonNode node = Json.parse(document); ObjectMapper m = new ObjectMapper(); Object jo; if (node instanceof ObjectNode) jo = m.convertValue(node, Map.class); else // (node instanceof ArrayNode) jo = m.convertValue(node, List.class); // JsonPath parses strings into java.util.Map and java.util.List // objects.
🌐
Maven Repository
mvnrepository.com › artifact › com.jayway.jsonpath › json-path
Maven Repository: com.jayway.jsonpath » json-path
February 22, 2026 - Jayway JsonPath · Links · JSON Libraries · JSON Query Libraries · JSON Schema Libraries · JSON-LD Libraries · JSON-RPC Libraries · Maven Plugins · Testing · Android Packages · Language Runtime · JVM Languages · Logging Frameworks · JSON Libraries · Java Specifications ·
🌐
Makeseleniumeasy
makeseleniumeasy.com › 2023 › 07 › 19 › __trashed
Replace Value in JSON Using JsonPath in Java | Jayway JsonPath | Rest Assured |
[ { "full name": "Everett Fritsch", "address": { "street": "898 Chesley View", "city": "Zing" }, "skills": [ "Maths", "Science" ] }, { "full name": "Stacey Witting", "address": { "street": "993 Keebler Summit", "city": "La Unión" }, "skills": [ "Java", "Python" ] } ] ... We will change the “full name” of the first element of the JSON array. ... We will replace all the “full name” in the array. @Test public void changeValueOfKeyUsingJsonPath() { // Replace value and converting new JSON to string jsonObject = JsonPath.parse(jsonObject).set("$[0]['full name']", "Amod Mahajan").jsonString
🌐
Java Guides
javaguides.net › 2024 › 05 › guide-to-jsonpath-library-in-java.html
Guide to JsonPath Library in Java
May 25, 2024 - JsonPath is a powerful library for querying and extracting data from JSON documents. Guide to JsonPath Library in Java with Examples. JsonPath.parse
🌐
Hevodata
docs.hevodata.com › sources › engg-analytics › streaming › rest-api › writing-jsonpath-expressions
Writing JSONPath Expressions - Hevo Data
JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document. You use a JSONPath expression to traverse the path to an element in the JSON structure. You start at the root node or element, represented by $, and reach the required ...
🌐
Maven Repository
mvnrepository.com › artifact › com.jayway.jsonpath
Maven Repository: com.jayway.jsonpath
com.jayway.jsonpath » json-path-parent Apache · Java JsonPath implementation · Last Release on Oct 1, 2014 · Central · Atlassian External · Atlassian · WSO2 Releases · WSO2 Public · Hortonworks · KtorEAP · Mulesoft · JCenter · Sonatype · 🌐 · DNS Gurus ·
🌐
Level Up Lunch
leveluplunch.com › java › examples › parse-json-elements-with-jsonpath
Json path example | Level Up Lunch
September 8, 2013 - Example shows how to extract elements from json. JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document.
🌐
Javadoc.io
javadoc.io › doc › io.rest-assured › json-path › latest › io › restassured › path › json › JsonPath.html
JsonPath - json-path 5.5.6 javadoc
Bookmarks · Latest version of io.rest-assured:json-path · https://javadoc.io/doc/io.rest-assured/json-path · Current version 5.5.6 · https://javadoc.io/doc/io.rest-assured/json-path/5.5.6 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/io.rest-...
🌐
Xing's Blog
xinghua24.github.io › Java › JsonPath
JSON Navigation with Jayway JsonPath | Xing's Blog
March 4, 2024 - It offers a concise and expressive way to traverse and manipulate JSON structures, making it a preferred choice for Java developers working with JSON data. To get started with Jayway JsonPath, you can include the following Maven dependency in your project: