JSONPath | Usage |
---|---|
$ | the root object/element |
@ | the current object/element |
. or [] | child operator |
.. | recursive descent. JSONPath borrows this syntax from E4X. |
* | wildcard. All objects/elements regardless their names. |
[] | subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator. |
?() | applies a filter (script) expression. |
() | script expression, using the underlying script engine. |
A JSONPath Tester is a tool used to test and evaluate JSONPath expressions against JSON data. JSONPath is a query language for JSON (JavaScript Object Notation), similar to XPath for XML. It allows you to navigate and extract specific data from JSON objects, arrays, and nested structures.
Common Use Cases:
Extracting Values: If you have a large JSON structure and need to extract specific values or parts of it (like an ID, name, or address), you can use JSONPath expressions.
Validating JSONPaths: You can check if a JSONPath expression works correctly against your data to ensure it returns the expected results.
Debugging: It helps debug and understand the results of your JSONPath expressions in real-time.
Example:
Consider this JSON object:
json
{
"store": {
"book": [
{ "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 }
]
}
}
You can use the following JSONPath expression to extract the titles of all books:
javascript
$.store.book[*].title
Features of a JSONPath Tester:
Test JSONPath Expressions: Enter your JSON data and JSONPath expression, and the tester will return the results.
Real-time Evaluation: Some testers update results instantly when you change the expression or JSON data.
Visual Feedback: It highlights parts of the JSON data that match the path, helping you visually debug your queries.
Support for Complex Queries: Many testers support advanced JSONPath features like filtering, multi-level queries, and array slicing.