1. Brief description
JSONPath is a powerful tool for querying and manipulating JSON data. Similar to SQL, it provides a simple and efficient solution for handling complex JSON data structures.
This article will introduce the basic syntax of JSONPath and demonstrate its practical application through detailed Java examples.
2. What is JSONPath?
JSONPath is a path expression language for JSON documents, similar to XPath (for XML). It allows us to:
- Extract values in JSON
- Filter and manipulate data
- Traversing nested structures
Basic syntax rules:
JSONPath expression | Function description |
---|---|
$ |
Root object |
. or[]
|
Access child elements |
* |
Wildcards, match all elements |
.. |
Recursive search |
?() |
Filter expressions |
@ |
Current Element |
Before using JSONPath, it needs to be added. Here are the Maven dependencies for JSONPath:
<dependency> <groupId></groupId> <artifactId>json-path</artifactId> <version>2.8.0</version> </dependency>
3. Java examples
In Java, we can use open source librariesJayway JSONPathTo implement the functions of JSONPath. The following example JSON data:
{ "store": { "book": [ { "category": "fiction", "author": "John", "price": 10.99 }, { "category": "science", "author": "Jane", "price": 12.99 }, { "category": "fiction", "author": "George", "price": 8.99 } ], "bicycle": { "color": "red", "price": 99.99 } } }
3.1 Basic Query
Get all the categories of books from JSON.
import ; import ; public class JSONPathDemo { public static void main(String[] args) { String json = """ { "store": { "book": [ { "category": "fiction", "author": "John", "price": 10.99 }, { "category": "science", "author": "Jane", "price": 12.99 }, { "category": "fiction", "author": "George", "price": 8.99 } ] } } """; List<String> categories = (json, "$.[*].category"); ("📚Book Category: " + categories); } }
Output:
Book Category: [fiction, science, fiction]
3.2 Filtering Query
Get books that cost more than 10.
List<Map<String, Object>> expensiveBooks = (json, "$.[?(@.price > 10)]"); ("💰Books with a price greater than 10: " + expensiveBooks);
Output:
Price is greater than 10 Books: [{category=fiction, author=John, price=10.99}, {category=science, author=Jane, price=12.99}]
3.3 Recursive search
Get all price fields.
List<Double> prices = (json, "$.store..price"); ("💵All Price: " + prices);
Output:
All prices: [10.99, 12.99, 8.99, 99.99]
3.4 Nested Query
Get the color of the bike.
String color = (json, "$."); ("🚲Bike Color: " + color);
Output:
Bicycle color: red
3.5 Use in combination with POJO
Map the query results into a Java object.
import ; class Book { private String category; private String author; private double price; // Getters and Setters } List<Book> books = (json).read("$.[*]", new TypeRef<List<Book>>() {}); (book -> ("📖 Books: " + () + " by " + ()));
4. Advanced usage
4.1 Dynamic path
Dynamically generate JSONPath expressions based on user input.
String category = "fiction"; String jsonPath = ("$.[?(@.category == '%s')]", category); List<Map<String, Object>> result = (json, jsonPath); ("🔍Query Results: " + result);
4.2 Custom functions
Implement complex logic by extending the functionality of JSONPath.
Configuration conf = () .options(Option.DEFAULT_PATH_LEAF_TO_NULL) .functions(new CustomFunctions()) .build(); DocumentContext context = (conf).parse(json);
5. Summary
JSONPath is a powerful tool for processing JSON data. Its intuitive syntax and powerful functions make JSON data operation easy and easy. By combining Java and third-party libraries, JSONPath can be applied efficiently in various scenarios.
The above is the detailed content of the technical guide for Java using JSONPath to operate JSON data. For more information about Java JSONPath to operate JSON data, please pay attention to my other related articles!