Subject: [Jsonpath] ["-"] Index Selector like JSON Pointer /-

From: Joel Bruner <[email-masked]>
Date: Tue, 8 Feb 2022 23:52:41 -0600

Hello,
So I had a couple ideas to float in the list here. If they belong in GitHub let me know.

I came upon JSONPath a couple years ago when I began work on a JSON tool for Mac admins (https://github.com/brunerd/jpt). I really liked the descendant selector to find a key somewhere in a JSON document, a common task in admin shell scripts. The JSONPath code I used was Stefan’s original JS code and did some additional work on it (https://github.com/brunerd/jsonpath)

My question: Since JSONPath is both useful for getting values and setting values, one should be able to specify the array element _after_  the last. JSONPath could specify that $["-"] symbolizes this, just as JSON Pointer has /- to signify this.

Section 3.5.4 "Index Selector" is where this behavior could be mentioned. The form ["-"] fits in the quoted member name rules and doesn't introduce any new syntax type. Here's how JSON Pointer RFC6901 describes /-
 
	https://tools.ietf.org/pdf/rfc6901.pdf - Section 4, Evaluation
	
	If the currently referenced value is a JSON array, the reference
	      token MUST contain either:
		…

	      *  exactly the single character "-", making the new referenced
	         value the (nonexistent) member after the last array element.

	Note that the use of the "-" character to index an array will always
	result in such an error condition because by definition it refers to
	a nonexistent array element.  Thus, applications of JSON Pointer need
	to specify how that character is to be handled, if it is to be
	useful.

As an example let’s add 3 to the end of the array with my jpt tool using both syntaxes:
% jpt -o add -v 3 -p /myArray/- <<< '{"myArray":[0,1,2]}'
{
  "myArray": [
    0,
    1,
    2,
    3
  ]
}

% jpt -o add -v 3 -p '$.myArray["-"]' <<< '{"myArray":[0,1,2]}'
{
  "myArray": [
    0,
    1,
    2,
    3
  ]
}

jpt has been a fun project to make a dependency free JSON tool for Mac (or any *nix with jsc installed) - thanks again Stefan Gössner for the original JS code, it was the simplest, leanest, non-sprawling, single file implementation I could find (sure the parser needed work but I made a weird regex based parser go reverse because jsc only has negative lookahead!). But yeah $["-"] what do folks think of that?

Thanks,
Joel

---

From: Greg Dennis <[email-masked]>
Date: Wed, 9 Feb 2022 06:04:18 +0000 (UTC)

"Since JSONPath is both useful for getting values and setting values"
I think this is the crucial point: JSON Path is intended as only a query language.  It's not intended to specify a location for days to be set/inserted.
That's not to say that it can't be used that way, but such usages are outside of the scope of the specification were writing.
Thanks for the idea, though. For now, I think JSON Pointer is probably a better syntax for your use case.
Greg

  On Wed, 9 Feb 2022 at 6:53 pm, Joel Bruner<[email-masked]> wrote:   Hello,
So I had a couple ideas to float in the list here. If they belong in GitHub let me know.

 

Thanks,
Joel

-- 
JSONpath mailing list
[email-masked]
https://www.ietf.org/mailman/listinfo/jsonpath

---

From: Carsten Bormann <[email-masked]>
Date: Wed, 9 Feb 2022 11:18:47 +0100

> My question: Since JSONPath is both useful for getting values and setting values, one should be able to specify the array element _after_  the last. JSONPath could specify that $["-"] symbolizes th…

It is a bit nonsensical to identify something that doesn’t exist.

The JSON Pointer /foo/- (assuming that /foo is an array) is really useful only as an instruction to append to the array identified by /foo.  In JSONPath, we can identify that array as $[‘foo’], but any append instruction needs to be out of band.

Thank you for this timely contribution:  We are in the process of adding text about JSON pointer in #152 [1], and we probably should say there what I just said (maybe in a new PR, as 152 is essentially finished).

Grüße, Carsten

[1]: https://github.com/ietf-wg-jsonpath/draft-ietf-jsonpath-base/pull/152

---

From: Joel Bruner <[email-masked]>
Date: Thu, 10 Feb 2022 00:11:39 -0600

> It is a bit nonsensical to identify something that doesn’t exist.

You know, this is also sounds like an important life lesson :]

> Thank you for this timely contribution ...

You are welcome, I figure if you "see something, say something"
Oh and regarding intentions…

> On Feb 9, 2022, at 12:04 AM, Greg Dennis <[email-masked]> wrote:
> 
> I think this is the crucial point: JSON Path is intended as only a query language.  It's not intended to specify a location for days [sic] to be set/inserted.
> 
> That's not to say that it can't be used that way, but such usages are outside of the scope of the specification we're writing.

Like a parent may have intentions for their children, life can often work out much differently! David Lynch's Eraserhead surrealistically captures what happens when your creation mutates! (Yikes)

For example, when I started making my jpt tool in 2020 I was just fooling around building a function to output (what are now being called) "Normalized Paths" and I realized since the syntax just follows ECMAScript/JS rules, the normalized path could be followed by an equal sign and the value (JSON or single quote string). It's kinda helpful for visual folks [1] and it's something JSON Pointer just can't do:

jpt -L <<< '{"results":[{"result":"abc"},{"result":123}]}' 
$={}
$["results"]=[]
$["results"][0]={}
$["results"][0]["result"]="abc"
$["results"][1]={}
$["results"][1]["result"]=123

Anyway thanks again for your responses and consideration to a comment from the peanut gallery :D
Joel

[1]: https://www.brunerd.com/blog/2022/02/01/jpt-see-json-differently/

---

From: Glyn Normington <[email-masked]>
Date: Thu, 10 Feb 2022 17:17:17 +0000

Thanks for taking this philosophically, Joel.

FYI my implementation ([2]) of JSONPath also accommodates updating the
nodes of a document selected by a JSONPath, but it does so by returning
pointers to nodes, so is somewhat lower-level than your approach.

[2] https://github.com/vmware-labs/yaml-jsonpath/

On Thu, 10 Feb 2022 at 06:11, Joel Bruner <[email-masked]> wrote:

> I think this is the crucial point: JSON Path is intended as only a query
> language.  It's not intended to specify a location for days [sic] to be
> set/inserted.
>
> That's not to say that it can't be used that way, but such usages are
> outside of the scope of the specification we're writing.
>
>
> Like a parent may have intentions for their children, life can often work
> out much differently! David Lynch's Eraserhead surrealistically captures
> what happens when your creation mutates! (Yikes)
>
> For example, when I started making my jpt tool in 2020 I was just fooling
> around building a function to output (what are now being called)
> "Normalized Paths" and I realized since the syntax just follows
> ECMAScript/JS rules, the normalized path could be followed by an equal sign
> and the value (JSON or single quote string). It's kinda helpful for visual
> folks [1] and it's something JSON Pointer just can't do:
>