JSONPath Complete Guide
What Is JSONPath?
JSONPath is a query expression language for extracting specific data from JSON documents, analogous to XPath for XML. JSONPath expressions let you locate any element in JSON using concise syntax โ regardless of how deeply nested it is โ without writing complex recursive code.
JSONPath was originally proposed by Stefan Goessner in 2007 and now has RFC 9535 standard (officially published in 2024) formalizing its syntax. It's widely used in various tools and systems: jq (command-line JSON processor), Kubernetes (resource config querying), Ansible (configuration management), various API testing frameworks, and more.
JSONPath Basic Syntax
JSONPath uses these basic symbols:
$ โ ๆ น่็น๏ผRoot element๏ผ
. โ ๅญๅ
็ด ่ฎฟ้ฎ็ฌฆ๏ผChild accessor๏ผ
.. โ ้ๅฝไธ้๏ผๆ็ดขๆๆๅไปฃ
* โ ้้
็ฌฆ๏ผๅน้
ๆๆๅญๅ
็ด
[] โ ไธๆ ๆไฝ็ฌฆ๏ผๆฐ็ป็ดขๅผใๅฑๆงๅใ่ฟๆปคๅจ๏ผ
[n] โ ่ฎฟ้ฎๆฐ็ป็ฌฌ n ไธชๅ
็ด ๏ผ0-based๏ผ
[*] โ ๆฐ็ปๆๆๅ
็ด
[start:end] โ ๆฐ็ปๅ็
[?(...)] โ ่ฟๆปคๅจ่กจ่พพๅผ
Practical Examples: Extracting Data from JSON
Given the following JSON data:
{
"store": {
"books": [
{"title": "The Great Gatsby", "price": 12.99, "category": "fiction"},
{"title": "Python Crash Course", "price": 29.99, "category": "tech"},
{"title": "Clean Code", "price": 34.99, "category": "tech"}
],
"owner": "Alice"
}
}
Common JSONPath queries:
$.store.owner โ "Alice"
$.store.books[0].title โ "The Great Gatsby"
$.store.books[*].price โ [12.99, 29.99, 34.99]
$.store.books[-1].title โ "Clean Code"๏ผๆๅไธๆฌ๏ผ
$..title โ ้ๅฝๆฅๆพๆๆ title ๅญๆฎต
$.store.books[1:3] โ ็ฌฌ 2ใ3 ๆฌไนฆ๏ผๅ็๏ผ
Filter Expressions: Conditional Queries
JSONPath's filter expression [?(...)] allows filtering array items by condition; @ represents the current element:
// ไปทๆ ผๅคงไบ 20 ็ไนฆ
$.store.books[?(@.price > 20)]
// ๅ็ฑปไธบ tech ็ไนฆ
$.store.books[?(@.category == 'tech')]
// ไปทๆ ผๅจ 10-30 ไน้ด็ไนฆ
$.store.books[?(@.price >= 10 && @.price <= 30)]
// ๆ็นๅฎๅญๆฎต็ๅ
็ด ๏ผๅญๆฎตๅญๅจๆงๆฃๆฅ๏ผ
$.store.books[?(@.isbn)]
JSONPath-Style Querying in jq
jq has its own query syntax, similar to but not identical with JSONPath. Common jq query examples:
# ๆๅๅญๆฎต
jq '.store.owner' data.json
# ๆฐ็ปๆๆๅ
็ด ็ๅญๆฎต
jq '.store.books[].title' data.json
# ่ฟๆปค๏ผไปทๆ ผ > 20
jq '.store.books[] | select(.price > 20)' data.json
# ๆๅ็นๅฎๅญๆฎตๅนถ้ๆ
jq '.store.books[] | {title, price}' data.json
# ่ๅ๏ผ่ฎก็ฎๆปไปท
jq '[.store.books[].price] | add' data.json
Using JSONPath in JavaScript
JavaScript doesn't have built-in JSONPath support; third-party libraries are needed:
// ไฝฟ็จ jsonpath-plus ๅบ
import jsonpath from 'jsonpath-plus';
const results = jsonpath.query(data, '$.store.books[?(@.price > 20)]');
console.log(results);
// ไฝฟ็จ @jsonpath/jsonpath-ts๏ผTypeScript ๅๅฅฝ๏ผ
import { JSONPath } from 'jsonpath-plus';
const titles = JSONPath({ path: '$..title', json: data });
Real-World JSONPath Applications
API Testing: In tools like Postman and Newman, use JSONPath expressions to extract specific values from responses for assertions, e.g., verify that $.data.user.id equals an expected value.
Kubernetes: The kubectl command supports JSONPath output format, e.g., kubectl get pods -o jsonpath='{.items[*].metadata.name}' extracts all Pod names.
Data extraction and transformation: In ETL (Extract-Transform-Load) workflows, use JSONPath to extract required fields from complex nested JSON and generate structured data.
Log analysis: Structured logs are typically recorded in JSON format; JSONPath queries can quickly extract specific fields from large volumes of logs for analysis.
Try the free tool now
Use Free Tool โ