4 ms·
`/` 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
by bsimpson 2y ago
`/` 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."
- yawnxyz 2y agothis 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
- dleeftink 2y agoExtending 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.
- yawnxyz 2y agoit'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
- bityard 2y agoMaybe we're talking about different things, but resources in REST are identified by their URL and URLs use '/' to separate elements in the path.
- bsimpson 2y agoYeah, but nobody ever looked at { "a": { "b": { "c": [] } } } and thought "I need the list at /a/b/c"
- cjblomqvist 2y agoIf you've ever done XPath you do!
- magicalhippo 2y agoYeah, 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.
- bombela 2y agoAnecdotal, I did and I do. It's no different than a path on a filesystem.
- stronglikedan 2y agoBut 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.
- maxbond 2y agoSeems 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."
- 2y ago
- DataOverload 2y agoIt 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 https://opis.io/json-schema/2.x/pointers.html
- Guthur 2y agoJSON 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.
- bsimpson 2y agoIs there any legal JSON that's not legal JavaScript? If not, it's fair to say it's a subset.
- tubthumper8 2y agoIt 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
- fanf2 2y agoJavaScript 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__"
- ks2048 2y agoI 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.
- graftak 2y agoNot 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.
- kevincox 2y agoDefinitely 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.
- PufPufPuf 2y agoYou would need to somehow differentiate between absolute and relative paths, though.
- svckr 2y agoYou'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 https://en.wikipedia.org/wiki/JSONPath
- tyre 2y agoand json path is supported in Postgres as a way to query json documents. It’s surprisingly full featured!
- xdennis 2y agoI 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 } }
- toomim 2y agoYeah, it sucks. It was borrowed from XPath. And it's not appropriate for JSON. https://en.wikipedia.org/wiki/XPath https://en.wikipedia.org/wiki/XPath
- PufPufPuf 2y agoAlso 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.
- krick 2y agoThat 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.