Skip to content

draft JSONPath documentation #19580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

draft JSONPath documentation #19580

wants to merge 5 commits into from

Conversation

taroface
Copy link
Contributor

@taroface taroface commented May 1, 2025

@taroface taroface requested review from dikshant and DrewKimball May 1, 2025 21:45
Copy link

github-actions bot commented May 1, 2025

Files changed:

Copy link

netlify bot commented May 1, 2025

Deploy Preview for cockroachdb-interactivetutorials-docs canceled.

Name Link
🔨 Latest commit daf6351
🔍 Latest deploy log https://app.netlify.com/sites/cockroachdb-interactivetutorials-docs/deploys/681d64edabc5e40008368767

Copy link

netlify bot commented May 1, 2025

Deploy Preview for cockroachdb-api-docs canceled.

Name Link
🔨 Latest commit daf6351
🔍 Latest deploy log https://app.netlify.com/sites/cockroachdb-api-docs/deploys/681d64ed2766a500087de2e4

Copy link

netlify bot commented May 1, 2025

Netlify Preview

Name Link
🔨 Latest commit daf6351
🔍 Latest deploy log https://app.netlify.com/sites/cockroachdb-docs/deploys/681d64edad0af00008628313
😎 Deploy Preview https://deploy-preview-19580--cockroachdb-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

A JSONPath expression has the following generic structure:

~~~
[mode] $[.key] [? (filter_exp)].[key].[method]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this diagram meant to show the allowed syntax for JSONPath? If it's just meant to be an example, maybe we should note that somewhere. Otherwise, it's not quite correct - for example, these are all valid JSONPath expressions:

root@localhost:26257/defaultdb> select '1'::jsonpath;
  jsonpath
------------
         1
(1 row)

root@localhost:26257/defaultdb> select '1 == 2'::jsonpath;
  jsonpath
------------
  (1 == 2)
(1 row)

root@localhost:26257/defaultdb> select '(100 * 200) ? ($foo > 100)'::jsonpath;
           jsonpath
------------------------------
  (100 * 200)?($"foo" > 100)
(1 row)

root@localhost:26257/defaultdb> select 'exists($)'::jsonpath;
   jsonpath
--------------
  exists ($)
(1 row)

Following the optional mode is a sequence of scalar expressions/predicates separated by accessors ., [...], and ?, with some restrictions about what can be in/following an accessor. The $ symbol is just a special expression referring to the root JSONB expression being queried.


- `mode` is an optional mode (`lax` or `strict`) that determines how structural mismatches are tolerated. If not specified, the mode is `lax`. Refer to [Structural error handling](#structural-error-handling).
- `$` is the root of the JSONPath, and must be the first element in a JSONPath expression. For an example, refer to [Access entire document](#access-entire-document).
- `.key` is an optional key accessor that refers to a named field in a `JSONB` object. To access array elements, use `.key[index]` or `.key[*]`. Each successive key accessor navigates one level deeper into the structure. `.*` matches all fields in the current object.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make mention of the array accessor as it's own thing, I think. For example, you can do this:

root@localhost:26257/defaultdb> SELECT jsonb_path_query('[1, 2, 3]', '$[0]');
  jsonb_path_query
--------------------
                 1
(1 row)

[mode] $[.key] [? (filter_exp)].[key].[method]
~~~

- `mode` is an optional mode (`lax` or `strict`) that determines how structural mismatches are tolerated. If not specified, the mode is `lax`. Refer to [Structural error handling](#structural-error-handling).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some other keywords/expressions we're missing:

  • @ is the "current" expression, representing the current result of path evaluation and only usable within a filter. Ex:
-- '@' will be set to 300, and the filter will pass.
root@localhost:26257/defaultdb> SELECT jsonb_path_query('{}', '(100 + 200) ? (@ * 2 == 600)');
  jsonb_path_query
--------------------
               300
(1 row)
  • LAST evaluates to the last element of the array currently being evaluated. Only usable within an array subscript. Ex:
root@localhost:26257/defaultdb> SELECT jsonb_path_query('[1, 2, 3]', '$[last]');
  jsonb_path_query
--------------------
                 3
(1 row)

(After reading further) I see we have some examples using these keywords below - maybe we could make note that the ones here are the ones allowed in the root JSONPath expression, while there are others allowed in the context of an array or filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DrewKimball Thanks, I was struggling with this section overall. I completely reworked how it presents the syntax -- please have a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants