โ† Back to Blog

JSONPath Complete Guide

2026-04-15 ยท 5 min read

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 โ†’