How to Query JSON Using JSONPath

Grady Johnson · November 3rd, 2023

JSONPath provides a convenient way to query, filter, and extract specific elements from JSON documents, making it an essential tool for developers and data engineers who regularly work with large JSON files. This article aims to demystify JSONPath by exploring its syntax and common use cases, and hopes to demonstrate its utility in the world of data processing.

What is JSONPath?

JSONPath is a query language for JSON that offers a simple yet powerful syntax to traverse JSON strucutres and extract data. JSONPath is akin to XPath, the query language for XML. JSONPath queries—also known as JSONPath expressions—are used to specify a path within a JSON document to reach the desired elements. These expressions are evaluated against the JSON data, and the result is the selected data or subdocument.

JSONPath Syntax

Let's delve into the basic components of JSONPath by exploring an example. In the remainder of this section, we will be using the following JSON document to run our JSONPath expressions against.

json
{
  "store": {
    "books": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ]
  }
}

Root Object

The root of a JSON document is denoted by the dollar sign $. All JSONPath expressions start with the root object.

Dot Notation

Dot notation is used to traverse through object properties. For example, $.store.books would select the value of the "books" property of the root object.

json
[
  {
    "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
  },
  {
    "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
  },
  {
    "category": "fiction",
    "author": "Herman Melville",
    "title": "Moby Dick",
    "isbn": "0-553-21311-3",
    "price": 8.99
  },
  {
    "category": "fiction",
    "author": "J. R. R. Tolkien",
    "title": "The Lord of the Rings",
    "isbn": "0-395-19395-8",
    "price": 22.99
  }
]

Bracket Notation

Bracket notation allows you to access array elements and object properties with special characters or spaces in their names. For instance, $.store.books[3] or $['store']['books'][3] selects the fourth element of the "books" array.

json
[
  {
    "category": "fiction",
    "author": "J. R. R. Tolkien",
    "title": "The Lord of the Rings",
    "isbn": "0-395-19395-8",
    "price": 22.99
  }
]

Wildcards

Wildcards * are useful for selecting multiple elements in an array or object. For example, $.store.books[*].title would select all the titles of the books contained within the "books" array.

json
[
  "Sayings of the Century",
  "Sword of Honour",
  "Moby Dick",
  "The Lord of the Rings"
]

Filters

Filters enable you to conditionally select elements based on their values. For instance, $.store.books[?(@.price>10)] selects books with a price greater than $10.

json
[
  {
    "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
  },
  {
    "category": "fiction",
    "author": "J. R. R. Tolkien",
    "title": "The Lord of the Rings",
    "isbn": "0-395-19395-8",
    "price": 22.99
  }
]

Recursive Descent

A recursive descent operator .. allows you to search for elements in nested objects without specifying the exact path. For example, $.store..author selects all "author" values in the JSON document under the "store" object, regardless of the depth.

json
[
  "Nigel Rees",
  "Evelyn Waugh",
  "Herman Melville",
  "J. R. R. Tolkien"
]

Common Use Cases for JSONPath

JSONPath is widely used in various fields, including web development, data engineering, and API integration. Here are some common scenarios where JSONPath proves invaluable:

  1. API Response Parsing: When working with RESTful APIs, JSON is often the format used for data exchange. JSONPath enables developers to extract specific information from API responses efficiently. For example, extracting the temperature from a weather API response or accessing user details from a user profile endpoint.
  2. Data Transformation: Data pipelines and ETL (Extract, Transform, Load) processes frequently involve converting JSON data into a different format or structure. JSONPath is used to navigate the input JSON, extract relevant data, and transform it into the desired output.
  3. Configuration Files: Many applications use JSON configuration files. JSONPath can be used to retrieve configuration parameters, making it easier to manage application settings and customizations.
  4. Web Scraping: In web scraping, JSON data is often used to store structured information on web pages. JSONPath can be employed to extract specific data from these web pages, such as product prices, reviews, or news articles.
  5. Data Validation: JSONPath can be used to validate and enforce data integrity by ensuring that JSON documents conform to predefined structures or contain specific values in expected places.

JSONPath Libraries and Tools

Various programming languages and libraries support JSONPath, simplifying its use in different contexts. Some of the popular libraries and tools include:

  • JavaScript: The jsonpath or jsonpath-plus packages allow you to work with JSONPath in JavaScript projects.
  • Python: The jsonpath-ng library offers JSONPath support in Python.
  • Java: The Jayway JsonPath library is widely used for JSONPath operations in Java applications.
  • Online Tools: There are online JSONPath evaluators that allow you to test your JSONPath expressions against sample JSON data without writing any code. This website—betterjson.com—is one such tool. You can execute JSONPath expressions after formatting your JSON by using the filter bar in the JSON viewer.
    A screenshot of the filter bar on betterjson.com
    The filter bar on betterjson.com enables users to query/filter their JSON using JSONPath.

Conclusion

JSONPath is a versatile and powerful tool for navigating, querying, and extracting data from JSON documents. Its simple yet expressive syntax allows developers and data engineers to work efficiently with JSON data, whether for API integration, data transformation, or any other scenario where JSON is involved.

Understanding the basic components of JSONPath, along with its various operators and use cases, is a valuable skill for anyone working with JSON data. With the help of JSONPath, you can harness the full potential of JSON as a data interchange format and make your data processing tasks more streamlined and efficient.