Most active commenters
  • bsimpson(4)

←back to thread

JSON Patch

(zuplo.com)
299 points DataOverload | 31 comments | | HN request time: 0.414s | source | bottom
1. bsimpson ◴[] No.41881312[source]
`/` is a weird choice of delimiter for JSON.

Since JSON is a subset of JS, I would have expected `.` to be the delimiter. That jives with how people think of JSON structures in code. (Python does require bracket syntax for traversing JSON, but even pandas uses dots when you generate a dataframe from JSON.)

When I see `/`, I think:

- "This spec must have been written by backend people," and

- "I wonder if there's some relative/absolute path ambiguity they're trying to solve by making all the paths URLs."

replies(9): >>41881377 #>>41881605 #>>41881615 #>>41882491 #>>41883303 #>>41883342 #>>41885101 #>>41885337 #>>41888089 #
2. yawnxyz ◴[] No.41881377[source]
this was one of the biggest learning curves / adjustments tbh but once I got over that it's surprisingly powerful.

It tackles like 80% of cases

replies(1): >>41881594 #
3. dleeftink ◴[] No.41881594[source]
Extending the 80/20 analogy, how much additional efforts does the last 20% take here? The format seems efficient enough, but I'm wondering about the complexity trade-offs one can expect.
replies(1): >>41890899 #
4. bityard ◴[] No.41881605[source]
Maybe we're talking about different things, but resources in REST are identified by their URL and URLs use '/' to separate elements in the path.
replies(2): >>41881871 #>>41887373 #
5. DataOverload ◴[] No.41881615[source]
It sort of is like a URL here's some more examples with both relative and absolute queries https://opis.io/json-schema/2.x/pointers.html
6. bsimpson ◴[] No.41881871[source]
Yeah, but nobody ever looked at

    {
      "a": {
        "b": {
          "c": []
        }
      }
    }
and thought "I need the list at /a/b/c"
replies(3): >>41881910 #>>41881957 #>>41882009 #
7. cjblomqvist ◴[] No.41881910{3}[source]
If you've ever done XPath you do!
replies(1): >>41882293 #
8. bombela ◴[] No.41881957{3}[source]
Anecdotal, I did and I do. It's no different than a path on a filesystem.
replies(1): >>41882617 #
9. maxbond ◴[] No.41882009{3}[source]
Seems like a reasonable thing to me.

    {
      "makes": {
        "toyota": {
          "models: [ ... ]
        }
      }
    }
"I need all the models of Toyota cars."

Or

"Toyota came out with a new Camry, I need to update the Camry object within Toyota's models."

replies(2): >>41882225 #>>41884316 #
10. bsimpson ◴[] No.41882225{4}[source]
Yeah, but that's makes.toyota.models, not /makes/toyota/models.

The point is that this is a data structure and not a web server. It's using a convention from one domain in a different one. Relatively minor in the scope of possible quibbles, but it's just one more thing to remember/one more trivial mistake to make. I'm sure much collective time will be lost to many people using dots where the spec says to use slashes, and not realizing it because it looks right in every other context with dots. Dots also makes copy/pasting easier, because you wouldn't have to migrate the format after pasting.

replies(2): >>41882298 #>>41886932 #
11. magicalhippo ◴[] No.41882293{4}[source]
Yeah, wrote my own XPath-like extension methods to manipulate JSON just like that. Felt very natural and makes it quite easy to generate and process JSON for the cases serialization/deserialization isn't the best option.
12. maxbond ◴[] No.41882298{5}[source]
Oh I see what you mean. I misunderstood, I also don't like slash as a separator.
13. Guthur ◴[] No.41882491[source]
JSON is derived from JavaScript, it is not a strict subset.

The most glaring issue is JSON number type versus JavaScript float. This causes issues in both directions whereby people consistently believe JSON can't represent numbers outside the float range and in addition JSON has no way to represent NaN.

replies(1): >>41882582 #
14. bsimpson ◴[] No.41882582[source]
Is there any legal JSON that's not legal JavaScript?

If not, it's fair to say it's a subset.

replies(2): >>41882750 #>>41882831 #
15. stronglikedan ◴[] No.41882617{4}[source]
But it's different than object notation in JS, and considering JSON stands for JavaScript Object Notation, I think dot notation would have been more appropriate for JSON Pointer (and by extension JSON Path). As a bit of a rebel myself, I use dot notation when describing a location in a JSON document, unless I'm forced to use the slash for something like JSON Pointer.
16. tubthumper8 ◴[] No.41882750{3}[source]
It is a subset as of JavaScript edition ES2019, when JavaScript strings are now allowed to contain U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR. Prior to ES2019, that was the only known example of legal JSON that was not legal JavaScript
17. fanf2 ◴[] No.41882831{3}[source]
JavaScript used to forbid U+2028 line separator and U+2029 paragraph separator in string literals, but JavaScript now allows them for compatibility with JSON.

The remaining wrinkle is different handling of "__proto__"

18. ks2048 ◴[] No.41883303[source]
I agree "." would make more sense than "/".

I actually think an array would be better, ["foo","bar"] for "foo.bar". How many many bugs are introduced by people not escaping properly? It's more verbose, but judging by the rest of the standard, they don't seem to be emphasizing brevity.

replies(3): >>41886443 #>>41887060 #>>41888096 #
19. svckr ◴[] No.41883342[source]
You're not the only one who thinks that!

JSON Patch uses JSON Pointer (RFC 6901) to address elements, but another method from (very) roughly same time is JSON Path [0] (RFC 9535) and here's one of my favorite mnemonics:

- JSON Path uses "points" between elements

- JSON Pointer uses "path separators" between elements

[0] https://en.wikipedia.org/wiki/JSONPath

replies(1): >>41885595 #
20. inkyoto ◴[] No.41884316{4}[source]
It quickly gets convoluted since Camry has been produced for more than 30 years with regular model refreshes throughout the years, e.g. 1997, 2003, 2025 etc. JSON Pointer quickly falls short since we now require a selection expression to figure out which year model we need to update in the array of models: «makes.toyota.models[?year == `2025`]» (using JMESPath).

Both, JSONPath or JMESPath, support query expressions whereas JSON Pointer does not.

21. xdennis ◴[] No.41885101[source]
I would have expected paths to be an array of string/number (e.g. ["persons", 3, "name"]) because keys can be any string.

Such a document may not be wise, but how would you update something like:

    { "charCounts": { "/": 1234, "a": 100, ".": 99 } }
22. toomim ◴[] No.41885337[source]
Yeah, it sucks. It was borrowed from XPath. And it's not appropriate for JSON.

https://en.wikipedia.org/wiki/XPath

23. tyre ◴[] No.41885595[source]
and json path is supported in Postgres as a way to query json documents. It’s surprisingly full featured!
24. graftak ◴[] No.41886443[source]
Not only that; both a dot (“.”) and a forward slash (“/”) are allowed to be used in/as a JSON property name.

An array of keys alleviates this issue entirely because any string element of the array can be used as a JSON object property name too.

25. diggan ◴[] No.41886932{5}[source]
> The point is that this is a data structure and not a web server.

URIs are not only used on web servers though, they're all over the place, probably most notable your filesystem is using `/` as a path separator, so it wouldn't be completely out of place to use it as a path separator elsewhere.

26. kevincox ◴[] No.41887060[source]
Definitely agree on the array. They were smart enough to use JSON for the patch format so that people didn't have to write custom parsers but then used a selector syntax that requires a custom parser (and serializer). It seems like an obvious unforced error.
27. ddgflorida ◴[] No.41887373[source]
When convertcsv.com converts JSON to CSV, it uses the / separator to store the structure in the column headings. Probably could use an option for . too.
28. PufPufPuf ◴[] No.41888089[source]
Also the escaping uses "~" as the escape character, and it escapes "~" and "/" as "~0" and "~1" instead of "~~" and "~/". This whole spec feels like it was written by aliens.
replies(1): >>41888798 #
29. PufPufPuf ◴[] No.41888096[source]
You would need to somehow differentiate between absolute and relative paths, though.
30. krick ◴[] No.41888798[source]
That I really don't like, it doesn't make any sense. "/" as a path delimiter feels totally ok though. I mean, it's path, after all. Also, I'd expect "." to be a part of a key much more often, than "/". And also it really doesn't matter what delimiter you use.
31. yawnxyz ◴[] No.41890899{3}[source]
it's really good for re-organizing schemas from for example Notion collections to a simple flat CSV / Google Sheets schema and Airtable

It's really bad at creating deeply nested schemas though, (eg from CSV -> Notion you'll get trouble). It's also limited in things like renaming keys, splitting values into multiple keys (eg a csv string into an array) and those sorts of things