How to Use JSONPath?
JSONPath is a powerful JSON query language that allows you to accurately select and extract data from JSON documents, similar to XPath for XML. It provides a flexible way to navigate and filter JSON structures.
JSON For You supports online JSONPath processing, just follow these steps:
- Paste the JSON string into the left editor;
- Click "Search Command" at the top, find "Show JSONPath filter box", and an input box will appear at the bottom after clicking;
- Enter a JSONPath expression to execute it automatically;
- You can view the results in the right editor;
- When you need to edit multiple times, you can quickly swap the text in the left and right editors with the
Ctrl+Enter
shortcut;
JSONPath Syntax Basics
Expression | Description | Example |
---|---|---|
$ | Root object | $ |
@ | Current object | @ |
| Child property | $.name |
| Subscript operator | $[0] |
| Wildcard (matches all elements) | $.store.book[*] |
| Recursive descent | $..price |
| Filter expression | $..book[?(@.price < 10)] |
| Script expression | $..book[(@.length-1)] |
JSONPath Practical Examples
Example 1: Basic Property Access
JSON data:
{
"name": "JSON For You",
"features": ["formatting", "validation", "JSONPath"]
}
JSONPath expression: $.name
, result: "JSON For You"
Example 2: Array Indexing
JSON data:
{
"users": [
{ "id": 1, "name": "John" },
{ "id": 2, "name": "Doe" }
]
}
JSONPath expression: $.users[1].name
, result: "Doe"
Example 3: Conditional Filtering
JSON data:
{
"products": [
{ "id": 1, "name": "Laptop", "price": 999 },
{ "id": 2, "name": "Mouse", "price": 25 },
{ "id": 3, "name": "Keyboard", "price": 50 }
]
}
JSONPath expression: $.products[?(@.price < 100)]
, result: returns the mouse and keyboard objects
Use Cases
- API Development: Extract specific fields from complex API responses
- Data Analysis: Filter and aggregate JSON data for reports
- Configuration Management: Retrieve settings from nested JSON configuration files
- Test Automation: Validate JSON responses in API tests
- Log Analysis: Extract meaningful information from JSON formatted logs