From 79412afc538481f029c8738a26069b94f31e0b0c Mon Sep 17 00:00:00 2001 From: Davide Bettio Date: Fri, 19 Mar 2021 13:10:47 +0100 Subject: [PATCH] Allow `@` as start selector Allow beginning paths using `@` instead of `$`. --- draft-ietf-jsonpath-base.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/draft-ietf-jsonpath-base.md b/draft-ietf-jsonpath-base.md index fdae1be1..8be89a8d 100644 --- a/draft-ietf-jsonpath-base.md +++ b/draft-ietf-jsonpath-base.md @@ -391,6 +391,7 @@ constant. | `//book[isbn]` | `$..book[?(@.isbn)]` | filter all books with isbn number | | `//book[price<10]` | `$..book[?(@.price<10)]` | filter all books cheaper than 10 | | `//*` | `$..*` | all elements in XML document; all members of JSON data item | +| `author` | `@.author` | `"J. R. R. Tolkien"` when fourth book is the current item | {: #tbl-example title="Example JSONPath expressions applied to the example JSON data item"} @@ -449,13 +450,16 @@ return an error. ## Syntax -Syntactically, a JSONPath consists of a root selector (`$`), which -selects the root node of a JSON value, followed by a possibly empty -sequence of *selectors*. +Syntactically, a JSONPath consists of either a root selector (`$`), which +selects the root node of a JSON value, or a current item selector (`@`), which +selects a child node of a JSON value, followed by a possibly empty sequence of +*selectors*. ~~~~ abnf -json-path = root-selector *selector +json-path = start-selector *selector +start-selector = root-selector / current-item-selector root-selector = "$" ; $ selects document root node +current-item-selector = "@" ; @ selects current node ~~~~ The syntax and semantics of each selector is defined below. @@ -517,6 +521,18 @@ This is the concatenation of three lists, two of length one containing `0`, `1`, As a consequence of this approach, if any of the selectors selects no nodes, then the whole JSONPath selects no nodes. +The current item selector `@` when used outside a filter behaves as the document +root selector unless a child node is given to the evaluator. +When a child node is used as current item, evaluator must select it as starting +node instead of the document root when an expression starting with `@` is +evaluated. + +When a child node is used as current item, the current item selector `@` selects +it as starting node instead of the document root. + +The root selector `$` should be prefered to the current item selector `@` when +a path relative to the current item is not required. + In what follows, the semantics of each selector are defined for each type of node.