What is JSONPath: JsonPath.parse()

In Java, there isn’t a built-in JsonPath.parse() method within the standard JSON libraries. However, you can use external libraries such as Jayway JsonPath to achieve similar functionality to parse and work with JSONPath expressions.

Introduction to JSONPath

JSONPath is a query language used to navigate the JSON (JavaScript Object Notation) data structures, particularly in scenarios involving APIs, data processing, configuration files, and more. It is similar to XPath for XML and allows you to extract and manipulate specific parts of a JSON document.

JSONPath Expressions

JSONPath expressions are query strings, resemble XPath expressions and provide a concise way to reference and filter JSON data.

There are two notational formats supported by JsonPath for representing nodes:

  • Dot notation : $.store.book[0].title
  • Bracket notation : $[‘store’][‘book’][0][‘title’]

$ represents the root node and all the path expressions start with it.

How to use JsonPath.parse() method

Let’s understand with an example of how you can use Jayway JsonPath to parse JSON and apply JSONPath expressions in Java:

Firstly add the Jayway JsonPath library to your project using Maven by adding the following dependency to your xml file:

    <version>2.6.0</version> <!-- Use the latest version available -->

After adding the dependency, here is an example of using the JsonPath.parse() method.

import com.jayway.jsonpath.JsonPath;

public class JsonPathExample {
    public static void main(String[] args) {
        // Your JSON data
        String json = "{\"store\": {\"book\": [{\"category\": \"fiction\",\"title\": \"Book 1\",\"author\": \"Author 1\"},{\"category\": \"fiction\",\"title\": \"Book 2\",\"author\": \"Author 2\"}]}}";

        // Parse the JSON
        Object document = JsonPath.parse(json).json();

        // Apply a JSONPath expression to extract data
        String titleOfFirstBook = JsonPath.read(document, "$.store.book[0].title");
        System.out.println("Title of the first book: " + titleOfFirstBook);

        // You can perform additional JSONPath queries as needed
        // For example, to get all book titles:
        System.out.println("All book titles:");
        JsonPath.read(document, "$.store.book[*].title").forEach(System.out::println);

Note – You have to adjust the JSONPath expressions according to your specific JSON structure and requirements.

Here is the explanation of the code.

Object document = JsonPath.parse(json).json();

In the above statement we use the JsonPath.parse(json) method from Jayway JsonPath to parse the JSON string and created a document object which represents the parsed JSON.

String titleOfFirstBook = JsonPath.read(document, "$.store.book[0].title");
System.out.println("Title of the first book: " + titleOfFirstBook);

Here JsonPath.read() function parse the json document on a specified JSONPath expression. This expression retrieves the title of the first book from the JSON structure. It assigns the title to the titleOfFirstBook variable and prints it.