What is JSONPath?

JSONPath is a query language for JSON documents that enables extracting and filtering data from complex JSON structures using path expressions similar to XPath for XML.

Quick Facts

Full NameJSONPath Query Language
Created2007 by Stefan Goessner
SpecificationOfficial Specification

How It Works

JSONPath provides a standardized syntax for navigating and querying JSON data structures. It uses dot notation and bracket notation to traverse nested objects and arrays, with support for wildcards, recursive descent, array slicing, and filter expressions. JSONPath expressions start with $ representing the root element, followed by path segments that navigate through the JSON structure. The language supports both simple property access and complex filtering operations, making it invaluable for extracting specific data from large JSON responses.

Key Characteristics

  • Uses $ as root element reference
  • Supports dot notation ($.store.book) and bracket notation ($['store']['book'])
  • Wildcard (*) matches all elements at current level
  • Recursive descent (..) searches all descendants
  • Array slicing with [start:end:step] syntax
  • Filter expressions with ?() for conditional selection
  • Script expressions with () for computed values

Common Use Cases

  1. Extracting specific fields from API responses
  2. Filtering arrays based on conditions
  3. Testing JSON structure in automated tests
  4. Data transformation pipelines
  5. Configuration file parsing

Example

loading...
Loading code...

Frequently Asked Questions

What is the difference between JSONPath and XPath?

JSONPath is for JSON documents while XPath is for XML documents. JSONPath uses $ as the root element and dot/bracket notation, while XPath uses / for path separation. JSONPath is simpler but less standardized than XPath, which has W3C specifications. Both support filtering, wildcards, and recursive searching.

How do I select all elements in a JSON array with JSONPath?

Use the wildcard operator [*] to select all elements in an array. For example, $.items[*] selects all items in the 'items' array. You can also use $.items[*].name to get a specific property from all array elements.

What does the recursive descent operator (..) do in JSONPath?

The recursive descent operator (..) searches through all descendants of the current element. For example, $..name finds all 'name' properties anywhere in the JSON document, regardless of their nesting level. This is useful when you don't know the exact path to a property.

How do I filter array elements based on conditions?

Use filter expressions with ?() syntax. For example, $.books[?(@.price < 10)] selects all books with price less than 10. The @ symbol refers to the current element being evaluated. You can use comparison operators (<, >, ==, !=) and logical operators (&&, ||).

Is JSONPath standardized across different programming languages?

JSONPath implementations vary across languages and libraries, as there was no formal standard until RFC 9535 in 2024. Different implementations may have slight syntax variations, especially for filter expressions and script expressions. Always check your specific library's documentation for supported features.

Related Tools

Related Terms

Related Articles