News for hackers. Stuff that matters.
Re:JSONB Has Landed in SQLite by M4v3R on Wednesday December 06, 2023 @12:45AM (#38540812)
I don’t know about Sqlite’s implementation but in Postgres JSONB is not 100% transparent to the application. One caveat I’ve encountered while working on an application that stored large JSON objects in Postgres initially as JSONB is that it doesn’t preserve object key order, i.e. the order of keys in an object when you store it will not match the order of keys when you retrieve said object. While for most applications this is not an issue the one I was working on actually did rely on the order of keys (which I am aware is a bad practice but this is how the system was designed) and suddenly we noticed that the app started misbehaving. Changing the column type from JSONB to JSON fixed the problem.
Re:JSONB Has Landed in SQLite by magicalhippo on Wednesday December 06, 2023 @12:50AM (#38540841)
Given that the order of the keys is specified as having no significance in regular JSON[1], this is out-of-spec usage.

If key order has to be preserved then a blob type would be a better fit, then you're guaranteed to get back what you wrote.

For example, SQLite says it stores JSON as regular text but MySQL converts it to an internal representation[2], so if you migrate you might be in trouble.

[1]: https://ecma-international.org/publications-and-standards/st...

[2]: https://dev.mysql.com/doc/refman/8.0/en/json.html

Re:JSONB Has Landed in SQLite by devit on Wednesday December 06, 2023 @08:13AM (#38543397)
You can also add a property to all objects with an array of keys in the order you want (if you can guarantee it won't conflict with existing properties, or can escape those), or turn them into a {k, o} array where k is the key array and o the object or a {k, v} array where v is a value array if you don't care about looking up keys.
Re:JSONB Has Landed in SQLite by fbdab103 on Wednesday December 06, 2023 @01:21AM (#38540963)
I suppose it is what it is, but if ordering matters, it is only JSON-like in appearance. json.org says:

>An object is an unordered set of name/value pairs.

Re:JSONB Has Landed in SQLite by clarkdave on Wednesday December 06, 2023 @05:38AM (#38542300)
Indeed! There is at least one other difference, which is that the presence of null characters will cause an error in `jsonb`, but not `json`:

# select '{ "id": "\u0000" }'::json;

-> { "id": "\u0000" }

# select '{ "id": "\u0000" }'::jsonb;

-> ERROR: unsupported Unicode escape sequence

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @11:57AM (#38546421)
The object key order thing is not a JSON spec compliance issue, but this one is. Either PG should store this escaped or use counted byte strings, but you can see why neither is desirable for a high-performance binary JSON format.
Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @11:54AM (#38546381)
Never ever count on object key ordering, or even on there being no duplicate keys in objects. If the JSON texts you're dealing with can be processed -or even just stored- by other code then you're in for pain if you make any assumptions about object key ordering / object key dups.

Once you accept this then it stops being a problem.