Home Correction: JSON Path vs JSON Pointer
Post
Cancel

Correction: JSON Path vs JSON Pointer

In my post comparing JSON Path and JSON Pointer, I made the following claim:

A JSON Pointer can be expressed as a JSON Path only when all of its segments are non-numeric keys.

Thinking about this a bit more in the context of the upcoming JSON Path specification, I realized that this only considers JSON Path segments that have one selector. If we allow for multiple selectors, and the specification does, then we can write /foo/2/bar as:

$.foo[2,'2'].bar

Why this works

The /2 segment in the JSON Pointer says

  • If the value is an array, choose the item at index 2.
  • If the value is an object, choose the value under property “2”.

So to write this as a path, we just need to consider both of these options.

  • If the value is an array, we need a [2] to select the item at index 2.
  • If the value is an object, we need a ["2"] to select the value under property “2”.

Since the value cannot be both an array and an object, having both of these selectors in a segment [2,"2"] is guaranteed not to cause duplicate selection, and we’re still guaranteed to get a single value.

Caveat

While this path is guaranteed to yield a single value, it’s still not considered a “Singular Path” according to the syntax definition in the specification.

I raised this to the team, and we ended up adding a note to clarify.

Summary

A thing that I previously considered impossible turned out to be possible.

I’ve added a note to the original post summarizing this as well as linking here.

This post is licensed under CC BY 4.0 by the author.

Parallel Processing in JsonSchema.Net

JsonNode's Odd API