DevLab
JSON

JSONPath Syntax: Query JSON Like XPath

Learn JSONPath syntax to query, filter, and extract data from nested JSON structures.

What is JSONPath?

JSONPath is a query language for JSON, analogous to XPath for XML. It lets you navigate and extract data from JSON structures using a concise syntax.

JSONPath is widely used in tools like AWS Step Functions, Kubernetes configurations, API testing tools, and data transformation pipelines.

Basic Syntax

  • $ — Root element
  • .key or ['key'] — Access a child key
  • [n] — Access array element at index n
  • [*] — Wildcard: all elements of array or object
  • .. — Recursive descent: search all descendants
  • [start:end] — Array slice
  • [?(@.condition)] — Filter expression

Examples

Given: {"store":{"books":[{"title":"Deep Work","price":15},{"title":"Atomic Habits","price":18}]}}

  • $.store.books[0].title → "Deep Work"
  • $.store.books[*].title → ["Deep Work", "Atomic Habits"]
  • $.store.books[?(@.price > 16)].title → ["Atomic Habits"]
  • $..price → [15, 18] (recursive)

Filter Expressions

Filter expressions use [?(@.field operator value)] syntax. The @ refers to the current element being evaluated.

  • [?(@.active == true)] — Where active is true
  • [?(@.price >= 10)] — Where price >= 10
  • [?(@.name =~ /pattern/)] — Where name matches a regex (implementation-specific)

Frequently Asked Questions

What is the difference between JSONPath and jq?

JSONPath is a query language specification used inside APIs, testing tools, and config systems like AWS Step Functions. jq is a standalone command-line tool with its own syntax for filtering and transforming JSON. JSONPath uses dot notation like $.store.books[0].title, while jq uses pipe-based filtering like .store.books[0].title. JSONPath is better when you need to embed a query in a configuration file or HTTP request; jq is better for ad-hoc terminal work and shell scripting.

Does JSONPath work the same in every language?

No. JSONPath has no single RFC-level standard (though IETF RFC 9535 was published in 2024), so implementations differ. For example, filter expressions like [?(@.price > 10)] are widely supported, but regex matching with =~ is available in some implementations (Jayway JsonPath for Java) and absent in others (Python jsonpath-ng). Always test your JSONPath expressions against the specific library your project uses.

Can JSONPath modify JSON data or only read it?

Standard JSONPath is read-only — it extracts data from a JSON document but does not modify it. If you need to update JSON in place, use a library like jsonpatch (RFC 6902), jq with assignment operators, or language-specific tools like Python's jsonpath-ng-ext which adds set operations on top of JSONPath queries.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines