Skip to content

Commit da3fd31

Browse files
committed
add correction post
1 parent 71aebdf commit da3fd31

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

_posts/2023/2023-04-16-paths-and-pointers.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ But remember that for this JSON Pointer, the `2` _could_ potentially select a `"
7272
> A JSON Pointer can be expressed as a JSON Path only when all of its segments are non-numeric keys.
7373
{: .prompt-info}
7474

75+
> EDITOR'S NOTE: I realized a few months after posting this that this only considers JSON Path segments with a single selector. If you allow for multiple selectors, you can certainly convert any JSON Pointer to JSON Path by including a numeric selector and a string selector: `/foo/2/bar``$.foo[2,'2'].bar`. More info [here](/posts/paths-and-pointers-correction/).
76+
{: .prompt-tip}
77+
7578
`/foo/bar` is equivalent to `$.foo.bar`, however, in general, JSON Paths and JSON Pointers are not interchangeable.
7679

7780
## When do I use which?
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: "Correction: JSON Path vs JSON Pointer"
3+
date: 2023-07-22 09:00:00 +1200
4+
tags: [json-path, json-pointer]
5+
toc: true
6+
pin: false
7+
---
8+
9+
In my [post](/posts/paths-and-pointers/) comparing JSON Path and JSON Pointer, I made the following claim:
10+
11+
> A JSON Pointer can be expressed as a JSON Path only when all of its segments are non-numeric keys.
12+
13+
Thinking about this a bit more in the context of the [upcoming JSON Path specification](/posts/json-path-spec/), 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:
14+
15+
```jsonpath
16+
$.foo[2,'2'].bar
17+
```
18+
19+
## Why this works
20+
21+
The `/2` segment in the JSON Pointer says
22+
23+
- If the value is an array, choose the item at index 2.
24+
- If the value is an object, choose the value under property "2".
25+
26+
So to write this as a path, we just need to consider both of these options.
27+
28+
- If the value is an array, we need a `[2]` to select the item at index 2.
29+
- If the value is an object, we need a `["2"]` to select the value under property "2".
30+
31+
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.
32+
33+
### Caveat
34+
35+
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.
36+
37+
I raised this to the team, and we ended up [adding a note](https://github.com/ietf-wg-jsonpath/draft-ietf-jsonpath-base/pull/482) to clarify.
38+
39+
## Summary
40+
41+
A thing that I previously considered impossible turned out to be possible.
42+
43+
I've added a note to the original post summarizing this as well as linking here.

0 commit comments

Comments
 (0)