Skip to main content
CheckTown
Dev Tools

JSONPath: jezyk zapytan do wydobywania danych z JSON

Opublikowano 7 min czytania
W tym artykule

What Is JSONPath?

JSONPath is a query language for extracting data from JSON documents. Just as XPath navigates XML documents, JSONPath navigates JSON structures using a concise expression syntax. It was formalized in RFC 9535 (February 2024) after years of informal specification.

JSONPath expressions start with $ (the root element) and use dot notation or bracket notation to traverse the document. The language supports wildcards, recursive descent, array slicing, and filter expressions — making it possible to extract deeply nested values with a single expression.

JSONPath Syntax

JSONPath provides several operators for navigating and filtering JSON structures.

  • Root ($) — every JSONPath expression starts with $ representing the root object or array of the JSON document
  • Dot notation ($.store.book) — access child properties by name, similar to JavaScript object access. Dots separate each level of nesting.
  • Bracket notation ($['store']['book']) — alternative syntax using quoted property names in brackets. Required when property names contain special characters or spaces.
  • Wildcard (*) — matches all elements at the current level. $.store.* returns all direct children of store; $[*] returns all elements of a root array.
  • Recursive descent (..) — searches all descendants at any depth. $..price finds every 'price' field anywhere in the document regardless of nesting level.
  • Array slices ([start:end:step]) — select a range of array elements. $[0:3] returns the first three elements; $[-2:] returns the last two; $[::2] returns every other element.

JSONPath Expression Examples

Here are practical examples showing how JSONPath expressions extract data from real JSON structures.

  • Extracting nested values — $.users[0].name retrieves the name of the first user. $.config.database.host gets a deeply nested configuration value.
  • Filtering arrays — $.users[?(@.age > 18)] returns all users older than 18. $.products[?(@.price < 50)] finds affordable products. Filters use @ to reference the current element.
  • Multi-level queries — $..author returns every author field at any depth. $.store.book[*].title returns all book titles. $.orders[*].items[*].sku extracts SKUs across all orders and items.

These expressions can be combined to create powerful queries. For example, $.store.book[?(@.price < 10)].title returns titles of all books cheaper than 10.

Wypróbuj za darmo — bez rejestracji

Testuj wyrazenia JSONPath →

Common Use Cases

JSONPath is used wherever structured data needs to be queried or extracted from JSON documents.

  • API response extraction — pull specific fields from complex API responses without writing custom parsing code. Test expressions against sample responses before coding.
  • Configuration parsing — extract values from JSON configuration files in CI/CD pipelines, deployment scripts, and infrastructure-as-code tools
  • Log analysis — query structured JSON logs to find specific events, error patterns, or performance metrics across large log files
  • Data transformation — use JSONPath in ETL pipelines to select, filter, and reshape JSON data during import/export operations

JSONPath vs JQ vs JMESPath

Several query languages target JSON documents. Each has different strengths and ecosystem support.

  • JSONPath — standard query syntax (RFC 9535), widely supported in programming libraries (Java, Python, JavaScript, PHP). Best for simple extraction and filtering. No built-in transformation.
  • JQ — command-line JSON processor with a powerful functional language. Supports transformation, computation, and output formatting. Best for shell scripting and command-line data processing.
  • JMESPath — query language used by AWS CLI and Boto3. Stricter syntax than JSONPath with built-in functions. Best for AWS ecosystem integration and programmatic querying.

Frequently Asked Questions

Does JSONPath use zero-based or one-based array indexing?

JSONPath uses zero-based array indexing, consistent with most programming languages. $[0] returns the first element, $[1] returns the second, and so on. Negative indices count from the end: $[-1] returns the last element, $[-2] returns the second-to-last.

How do filter expressions work in JSONPath?

Filter expressions use the syntax [?(expression)] where @ refers to the current element being evaluated. You can use comparison operators (==, !=, <, >, <=, >=), logical operators (&& and ||), and property access (e.g., @.name). Example: $[?(@.status == 'active')] returns elements where status equals 'active'.

Are all JSONPath implementations compatible?

Not entirely. Before RFC 9535 (2024), JSONPath had no formal standard and implementations varied. Common differences include filter expression syntax, recursive descent behavior, and handling of missing properties. RFC 9535 aims to standardize behavior, but older libraries may still follow different conventions. Always test expressions against your specific implementation.

Powiązane narzędzia