How to Query JSON Using JSONPath
Grady Johnson · November 3rd, 2023 · Last Updated: March 14th, 2025JSONPath offers a powerful and intuitive way to query, filter, and extract specific elements from JSON documents, making it an invaluable tool for software developers and data engineers who regularly work with JSON. This article aims to demistify JSONPath by breaking down its syntax, exploring common use cases, and showcasing its practicality in data processing.
What is JSONPath?
JSONPath is a query language for JSON that provides a simple yet powerful syntax for navigating and extracting data from JSON documents. Similar to XPath for XML, JSONPath queries—also referred to as JSONPath expressions—define paths within a JSON document to locate specific elements. When evaluated against JSON data, these expressions return the selected values or subdocuments
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.
{
"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.
[
{
"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.
[
{
"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.
[
"Sayings of the Century",
"Sword of Honour",
"Moby Dick",
"The Lord of the Rings"
]
Array Slicing
JSONPath supports array slicing, allowing you to extract specific subsets of an array using a familiar range notation. The slicing syntax follows the format [start:end:step]
.
- start (optional) - The index where the slice begins (inclusive).
- end (optional) - The index where the slice ends (exclusive).
- step (optional) - The increment between elements in the slice.
If a value is omitted, JSONPath uses default values:
start
defaults to the beginning of the array (0).end
defaults to the length of the array.step
defaults to 1.
Using our JSON store example, the JSONPath expression $.store.books[0:2]
would return the first two 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
}
]
$.store.books[::2]
would return the first and third books.
[
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
}
]
And $.store.books[-3:]
, which uses a negative index, would return the last three books in the array.
[
{
"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
}
]
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.
[
{
"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.
[
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
Common Use Cases for JSONPath
JSONPath is used in various fields, including software development and data engineering. Here are some common scenarios where JSONPath proves invaluable:
- 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.
- 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 and extract relevant data.
- 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.
- 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.
- 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.
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.