News for hackers. Stuff that matters.
Re:JSONB Has Landed in SQLite by luhn on Wednesday December 06, 2023 @12:30AM (#38540754)
Lots of confusion on what JSONB is.

To your application, using JSONB looks very similar to the JSON datatype. You still read and write JSON strings—Your application will never see the raw JSONB content. The same SQL functions are available, with a different prefix (jsonb_). Very little changes from the application's view.

The difference is that the JSON datatype is stored to disk as JSON, whereas the JSONB is stored in a special binary format. With the JSON datatype, the JSON must be parsed in full to perform any operation against the column. With the JSONB datatype, operations can be performed directly against the on-disk format, skipping the parsing step entirely.

If you're just using SQLite to write and read full JSON blobs, the JSON datatype will be the best pick. If you're querying or manipulating the data using SQL, JSONB will be the best pick.

Re:JSONB Has Landed in SQLite by tuyiown on Wednesday December 06, 2023 @04:24AM (#38541930)
> If you're just using SQLite to write and read full JSON blobs, the JSON datatype will be the best pick.

I don't know how the driver is written, but this is misleading if sqlite provides an api to read the jsonb data with a in-memory copy, an app can surely benefit from skipping the json string parsing.

Re:JSONB Has Landed in SQLite by tyingq on Wednesday December 06, 2023 @04:26AM (#38541940)
> Your application will never see the raw JSONB content.

That's not exactly right, as the jsonb_* functions return JSONB if you choose to use them.

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @12:17PM (#38546739)
And you can just read the BLOB column values out of the DB.
Re:JSONB Has Landed in SQLite by nikeee on Wednesday December 06, 2023 @03:58AM (#38541783)
If my application never sees a difference top JSON, everything is compatible and all there is are perf improvements, why ist there a new set of functions top interact with it (jsonb_*)?

It seems that the JSON type is even able to contain JSONB.

Re:JSONB Has Landed in SQLite by dtech on Wednesday December 06, 2023 @04:14AM (#38541868)
The difference is the storage format, which is important for a SQL database since you define it in the schema. Also the performance characteristics are different.
Re:JSONB Has Landed in SQLite by The_Colonel on Wednesday December 06, 2023 @04:35AM (#38541995)
As someone mentioned below, the order of keys is undefined in JSON spec, but applications may rely on it anyway, and thus conversion to JSONB may lead to breakage.

There are some other minor advantages of having exact representation of the original - e.g. hashing, signatures, equality comparison is much simpler on the JSON string (you need a strict key order, which is again undefined by the spec, but happens in the real world anyway).

Re:JSONB Has Landed in SQLite by 0x457 on Wednesday December 06, 2023 @01:49PM (#38547964)
`json_` functions return json, `jsonb_` functions return jsonb. Both take either as input.

If you're modifying something "in-place" then `jsonb_` functions would be better since they avoid conversion.

Re:JSONB Has Landed in SQLite by blowski on Wednesday December 06, 2023 @12:56AM (#38540861)
Is there any downside to storing JSON-B even if you’re not planning to query it? For example, size on disk, read/write performance?
Re:JSONB Has Landed in SQLite by oefrha on Wednesday December 06, 2023 @01:10AM (#38540917)
There’s processing to be done with JSONB on every read/write, which is wasted if you’re always reading/writing the full blob.
Re:JSONB Has Landed in SQLite by lifthrasiir on Wednesday December 06, 2023 @02:12AM (#38541224)
Which occurs with JSON as well (SQLite doesn't have a dedicated JSON nor JSONB type). The only actual cost would be the conversion between JSONB and JSON.
Re:JSONB Has Landed in SQLite by oefrha on Wednesday December 06, 2023 @02:39AM (#38541357)
No, SQLite's "JSON" is just TEXT, there's no overhead with reading/writing a string.
Re:JSONB Has Landed in SQLite by lifthrasiir on Wednesday December 06, 2023 @02:53AM (#38541453)
That's what I said I think? "JSON" is a TEXT that is handled as a JSON string by `json_*` functions, while "JSONB" is a BLOB that is handled as an internal format by `jsonb_*` functions. You generally don't want JSONB in the application side though, so you do need a conversion for that.
Re:JSONB Has Landed in SQLite by akira2501 on Wednesday December 06, 2023 @03:20AM (#38541603)
Yes, but when you use the BLOB with jsonb functions, your application demands go from:

Read JSON from TEXT column.

Parse JSON into Internal Binary Format.

Run json_*() function on this format, which will Serialize Internal Binary Format to JSON as output.

To:

Read JSONB from BLOB column.

Run json_*() function on Internal Binary Format, which will serialize the Internal Binary Format to JSON as output.

Because:

The json_* and jsonb_* all accept _either_ JSON or JSONB as their input. The difference is jsonb_* functions also produces it as output. So even in the above case, if your function output is just being used to feed back into another table as a BLOB, then you can use the jsonb_* version of the function and skip the serialization step entirely.

Re:JSONB Has Landed in SQLite by lifthrasiir on Wednesday December 06, 2023 @03:22AM (#38541615)
Oh, you are right! I later looked at the draft documentation and realized that `json(b)_` only determines the output type. That said, it is still true that you need one more function call `json(...)` to retrieve a textual form of JSON which you do often need instead of JSONB.
Re:JSONB Has Landed in SQLite by Someone on Wednesday December 06, 2023 @10:45AM (#38545346)
> Run json_*() function on Internal Binary Format, which will serialize the Internal Binary Format to JSON as output

I don’t think that’s always correct (it is within this thread, which stated (way up) “if you’re always reading/writing the full blob”, but I think this discussion is more general by now). Firstly, it need not convert the full jsonb blob (example: jsonb_extract(field,'$.a.b.c.d.e'))

Secondly, it need not convert to text at all, for example when calling a jsonb_* function that returns jsonb.

(Technically, whether any implementation is that smart is an implementation detail, but the article claims this is

“a rewrite of the SQLite JSON functions that, depending on usage patterns, could be several times faster than the original JSON functions”

so it can’t be completely dumb.

Re:JSONB Has Landed in SQLite by oefrha on Wednesday December 06, 2023 @02:59AM (#38541494)
There’s no point using json_* functions if you’re always reading/writing the full blob.
Re:JSONB Has Landed in SQLite by lifthrasiir on Wednesday December 06, 2023 @03:09AM (#38541547)
Of course (and I never said that), but if you need to store a JSON value in the DB and use a JSONB-encoded BLOB as an optimization, you eventually read it back to a textual JSON. It's just like having a UNIX timestamp in your DB and converting back to a parsed date and time for application uses, except that applications may handle a UNIX timestamp directly and can't handle JSONB at all.
Re:JSONB Has Landed in SQLite by nonethewiser on Wednesday December 06, 2023 @10:50AM (#38545432)
It doesnt validate that its valid json?
Re:JSONB Has Landed in SQLite by ElectricalUnion on Wednesday December 06, 2023 @02:47PM (#38548591)
No, it doesn't. By default in sqlite3, you can store whatever you like in whatever field you want.

The "built-in validation mechanism" is invoking json_valid(X) [1] call within the CHECK [2] condition on a column.

[1] https://www.sqlite.org/json1.html#jvalid

[2] also assumes you didn't disable CHECKs with PRAGMA ignore_check_constraints https://www.sqlite.org/pragma.html#pragma_ignore_check_const...

Re:JSONB Has Landed in SQLite by jamesfinlayson on Wednesday December 06, 2023 @01:09AM (#38540916)
If the order of items in the JSON blob matters then JSONB probably wouldn't preserve the order.
Re:JSONB Has Landed in SQLite by mike_d on Wednesday December 06, 2023 @01:11AM (#38540923)
JSON is unordered. Nothing in your code should assume otherwise.

"An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array."

Re:JSONB Has Landed in SQLite by hakunin on Wednesday December 06, 2023 @01:47AM (#38541086)
That’s exactly the kind of difference between json and jsonb that you gotta keep in mind. Object properties are unordered, but a json string is very much ordered. It’s the same sequence of characters and lines each time, unless you parse it and dump it again. So if you want to preserve an unmodified original json string for some (e.g. cosmetic) reasons, you probably want json.
Re:JSONB Has Landed in SQLite by rowanG077 on Wednesday December 06, 2023 @04:36AM (#38542001)
I would expect you are threading on dangerous grounds to assume a type called JSON is going to preserve the data byte for byte. It might currently but I doubt that is in the API contract. You really want to use TEXT if that is your requirement
Re:JSONB Has Landed in SQLite by cztomsik on Wednesday December 06, 2023 @05:14AM (#38542204)
I don't know what SQLite does, but in JS the order is actually defined. JS is not Java, object is not HashMap, if anything, it's closer to LinkedHashMap, but even that is not correct because there are numeric slots which always go first. https://tc39.es/ecma262/#sec-ordinaryownpropertykeys
Re:JSONB Has Landed in SQLite by dspillett on Wednesday December 06, 2023 @05:44AM (#38542318)
> but in JS the order is actually defined

Defined yes, but still arbitrary and may not be consistent between JSON values of the same schema, as per the document you linked to:

> in ascending chronological order of property creation

Also, while JSON came from JS sort-of, it is, for better or worse (better than XML!) a standard apart from JS with its own definitions and used in many other contexts. JSON as specified does not have a prescribed order for properties, so it is not safe to assume one. JS may generally impose a particular order, but other things may not when [re]creating a JSON string from their internal format (JSONB in this case), so by assuming a particular order will be preserved you would be relying on a behaviour that is undefined in the context of JSON.

Re:JSONB Has Landed in SQLite by cztomsik on Wednesday December 06, 2023 @06:54AM (#38542702)
Yes, I know, the point was not to say that it's safe to depend on this universally, but rather why it's safe in JS and why it's not elsewhere -> other languages use hash maps simply because authors were either lazy or unaware of the original behaviour. (which sucks, in my opinion, but nobody can fix it now)
Re:JSONB Has Landed in SQLite by ElectricalUnion on Wednesday December 06, 2023 @03:06PM (#38548819)
> other languages use hash maps simply because authors were either lazy or unaware

Because it is idiomatic in that language, and you often don't need the higher overhead of tracking insertion order.

Before Python 3.7, json.loads used dict (undefined order) and you needed to explicitly need to override the load calls with the kwarg `object_pairs_hook=collections.OrderedDict` to accept ordered dictionaries.

Since Python 3.7 all `dict`s are effectively `collections.OrderedDict` because people now expect this kind of inefficient default behavior everywhere. ¯\_(ツ)_/¯

Re:JSONB Has Landed in SQLite by cdogl on Wednesday December 06, 2023 @01:48AM (#38541092)
The same goes for maps in Go, which now explicitly randomizes map iteration with the range keyword to prevent developers from relying on a particular ordering. Neat trick.
Re:JSONB Has Landed in SQLite by mike_hock on Wednesday December 06, 2023 @06:47AM (#38542664)
That might get them to rely on the randomization, though :)
Re:JSONB Has Landed in SQLite by plq on Wednesday December 06, 2023 @02:22AM (#38541280)
You make it sound like it's one of the laws of physics.

ON part of JSON doesn't know about objects. When serialized, object entries are just an array of key-value pairs with a weird syntax and a well-defined order. That's true for any serialization format actually.

It's the JS part of JSON that imposes non-duplicate keys with undefined order constraint.

You are the engineer, you can decide how you use your tools depending on your use case. Unless eg. you need interop with the rest of the world, it's your JSON, (mis)treat it to your heart's content.

Re:JSONB Has Landed in SQLite by mike_d on Wednesday December 06, 2023 @02:35AM (#38541334)
> You make it sound like it's one of the laws of physics.

The text I quoted is from the RFC. json.org and ECMA-404 both agree. You are welcome to do whatever you want, but then it isn't JSON anymore.

Re:JSONB Has Landed in SQLite by The_Colonel on Wednesday December 06, 2023 @04:27AM (#38541945)
That does not follow.

JSON formatted data remains JSON no matter if you use it incorrectly.

This happens all the time in the real world - applications unknowingly rely on undefined (but generally true) behavior. If you e.g. need to integrate with a legacy application where you're not completely sure how it handles JSON, then it's likely better to use plain text JSON.

You may take the risk as well, but then good luck explaining that those devs 10 years out of the company are responsible for the breakage happening after you've converted JSON to JSONB.

In some cases, insisting on ignoring the key order is too expensive luxury, since it basically forces you to parse the whole document first and only then process it. In case you have huge documents, you have to stream-read and this often implies relying on a particular key order (which isn't a problem since those same huge documents will likely be stream-written with a particular order too).

Re:JSONB Has Landed in SQLite by throwaway290 on Wednesday December 06, 2023 @04:33AM (#38541976)
It is very much still JSON, and your code can very much assume keys are ordered if your JSON tools respect it. ECMA-404 agrees:

> The JSON syntax does not impose any restrictions on the strings used as names, does not require that name strings be unique, and does not assign any significance to the ordering of name/value pairs. These are all semantic considerations that may be defined by JSON processors or in specifications defining specific uses of JSON for data interchange.

If you work in environments that respect JSON key order (like browser and I think also Python) then unordered behavior of JSONB would be the exception not the rule.

Re:JSONB Has Landed in SQLite by sltkr on Wednesday December 06, 2023 @07:18AM (#38542871)
> It's the JS part of JSON that imposes non-duplicate keys with undefined order constraint.

Actually since ES2015 the iteration order of object properties is fully defined: first integer keys, then string keys in insertion order, finally symbols in insertion order. (Of course, symbols cannot be represented in JSON.)

And duplicate properties in JSON are guaranteed to be treated the same way they are in object literals: a duplicate overwrites the previous value but doesn't change the order of the key.

Concretely that means if you write:

    Object.entries(JSON.parse('{"a":10, "1":20, "b":30, "a":40}'))
This is guaranteed to evaluate to:

    [['1', 20], ['a', 40], ['b', 30]]
(Note that '1' was moved to front, and 'a' comes before 'b' even though the associated value comes from the final entry in the JSON code.)

Python made a similar change in version 3.6 (officially since 3.7), both with regards to insertion order and later values overwriting earlier ones while preserving order. I think the only difference at this point is that Python doesn't move integer-like keys to the front, because unlike JavaScript, Python properly distinguishes between different key types.

Re:JSONB Has Landed in SQLite by iskela on Wednesday December 06, 2023 @03:07PM (#38548831)
All the keys in JSON are strings. I do not understand why string literal containing number would be moved first. Maybe in JS-object in runtimes.
Re:JSONB Has Landed in SQLite by jamesfinlayson on Wednesday December 06, 2023 @02:03AM (#38541174)
I don't disagree, but people might still assume it. If you serialise a Map in Java, some Map implementations will maintain insertion order for example.
Re:JSONB Has Landed in SQLite by ifwinterco on Wednesday December 06, 2023 @07:35AM (#38543023)
I think this is why JS and Python chose to make key ordering defined - the most popular implementations maintained insertion order anyway, so it was inevitable people would end up writing code relying on it
Re:JSONB Has Landed in SQLite by ako on Wednesday December 06, 2023 @09:05AM (#38544038)
Assume you're writing an editor for JSON files. Don't think many users of that editor would be very happy if you change the order of the attributes in their json files, even though technically it's the same...
Re:JSONB Has Landed in SQLite by jchanimal on Wednesday December 06, 2023 @09:47AM (#38544576)
According to the JSON spec it's also legal to round all numbers to zero.
Re:JSONB Has Landed in SQLite by golergka on Wednesday December 06, 2023 @10:21AM (#38544990)
If you’re writing an editor, you will need to separate in progress text that user is editing from the parsed and validated data, and keep both.
Re:JSONB Has Landed in SQLite by ash on Wednesday December 06, 2023 @01:29PM (#38547726)
SQLite JSONB does maintain order: https://news.ycombinator.com/item?id=38547254
Re:JSONB Has Landed in SQLite by chippiewill on Wednesday December 06, 2023 @05:26AM (#38542250)
Not specifically JSONB, but I do recall that with MongoDB's equivalent, BSON, the sizes of the binary equivalent tend to be larger in practice, I would expect JSONB to have a similar trade off.

There'll also be a conversion cost if you ultimately want it back in JSON form.

Re:JSONB Has Landed in SQLite by cowsandmilk on Wednesday December 06, 2023 @05:37AM (#38542298)
From the post:

> JSONB is also slightly smaller than text JSON in most cases (about 5% or 10% smaller) so you might also see a modest reduction in your database size if you use a lot of JSON.

Re:JSONB Has Landed in SQLite by kevincox on Wednesday December 06, 2023 @10:51AM (#38545461)
According to the linked announcement the data size is 5-10% smaller. So you are exchanging a small processing cost for smaller storage size. It will depend on your application but in many cases smaller disk reads and writes as well as more efficient cache utilization will make storing as JSONB a better choice even if you never manipulate it in SQL. Although the difference is likely small.
Re:JSONB Has Landed in SQLite by Someone on Wednesday December 06, 2023 @10:59AM (#38545602)
I haven’t delved into it, but you likely lose the ability to round-trip your json strings, just as in postgreSQL’s jsonb type.

  Jsonb_extract(jsonb(foo), '€')
likely

- will remove/change white space in ‘foo’,

- does not guarantee to keep field order

- will fail if a dictionary in the ‘foo’ string has duplicate fields

- will fail if ‘foo’ doesn’t contain a valid json string

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @12:16PM (#38546728)
PG's JSONB is compressible yet also indexed (objects' key/value pairs are sorted on key and serialized array-like; arrays are indexed by integers; therefore binary searching large objects works). The history of PG's JSONB type is very interesting.
Re:JSONB Has Landed in SQLite by lovasoa on Wednesday December 06, 2023 @04:36AM (#38542002)
> If you're just using SQLite to write and read full JSON blobs, the JSON datatype will be the best pick.

There is no json data type ! If you are just storing json blobs, then the BLOB or TEXT data types will be the best picks.

Re:JSONB Has Landed in SQLite by gwd on Wednesday December 06, 2023 @05:22AM (#38542234)
In context, what is clearly meant is, "If you're just reading and writing full JSON blobs, use `insert ... ($id,$json)`; if you're primarily querying the data, use `insert ... ($id, jsonb($json))`, which will convert the text into JSONB before storing it.
Re:JSONB Has Landed in SQLite by dheera on Wednesday December 06, 2023 @09:45AM (#38544547)
> There is no json data type

Why not? I feel like a database should allow for a JSON data type that stores only valid JSON or throws an exception. It would also be nice to be able to access subfields of the JSON.

SELECT userid, username, some_json_field["some_subfield"][0] FROM users where ...

Not sure where to give feature suggestions so I'm just leaving this here for future devs to find

Re:JSONB Has Landed in SQLite by sroussey on Wednesday December 06, 2023 @10:17AM (#38544930)
SQLite has a surprisingly small number of types: null, integer, real, text, and blob. That’s it.

https://www.sqlite.org/datatype3.html

Re:JSONB Has Landed in SQLite by mdaniel on Wednesday December 06, 2023 @12:42PM (#38547108)
The H2 database uses almost that exact syntax:

  create table foo(my_json json);
  insert into foo values ('{"a":{"b":{"c":"d"}}}' FORMAT JSON);
  select (my_json)."a"."b"."c" from foo; -- where the () around the field is mandatory
https://h2database.com/html/grammar.html#field_reference
Re:JSONB Has Landed in SQLite by chungy on Wednesday December 06, 2023 @12:45PM (#38547143)
> It would also be nice to be able to access subfields of the JSON.

Not that it's not _useful_ sometimes, but it amuses me that this is a huge violation of 1NF and people are often ok with it. It really depends on whether you're treating the JSON object as an atomic unit on its own, regardless of contents, or using the JSON to store more fine-grained information.

I guess the same argument can be made for XML data types, and everyone's OK with it too.

Re:JSONB Has Landed in SQLite by yencabulator on Wednesday December 06, 2023 @07:31PM (#38551369)
1NF is a theory construct that makes no accommodation for real-world performance. I'm shuddering even thinking how many tables and joins I would need to store some of these 3rd party JSON things I need to import & refine.
Re:JSONB Has Landed in SQLite by icedchai on Wednesday December 06, 2023 @07:07PM (#38551204)
Database design is, unfortunately, a lost art. Up through the early 2010's I remember having design reviews for database schemas, etc. That isn't "agile"... so you just fix it in the next sprint, as you explain to someone what a unique constraint is and why a table is now full of duplicates.
Re:JSONB Has Landed in SQLite by marcosdumay on Wednesday December 06, 2023 @01:29PM (#38547732)
The principles of relational algebra aren't always compatible with real applications.

They are successful beyond anything that I can imagine people expecting when creating them. But they are not that complete silver bullet that solves every problem humanity will ever need solved.

Re:JSONB Has Landed in SQLite by 0x457 on Wednesday December 06, 2023 @01:40PM (#38547863)
SQLite has a very few actual data types. There is a function that validates json and you can use it as a check on your `TEXT` or` `BLOB` column.

You can access subfields using `json_` and `jsonb_` functions.

Re:JSONB Has Landed in SQLite by zmmmmm on Wednesday December 06, 2023 @05:30AM (#38542271)
I don't know if it's true of SQLite, but you missed the most important point at least with PostgreSQL : you can build indexes directly against attributes inside JSONB which really turns it into a true NoSQL / relational hybrid that can let you have your cake and eat it too for some design problems that don't fit neatly into either pure relational or pure NoSQL approaches.
Re:JSONB Has Landed in SQLite by debugnik on Wednesday December 06, 2023 @07:26AM (#38542945)
You can do this with text JSON in SQLite as well, but JSONB could speed it up.
Re:JSONB Has Landed in SQLite by euroderf on Wednesday December 06, 2023 @05:54AM (#38542381)
What would that SQL look like in practice ?
Re:JSONB Has Landed in SQLite by ako on Wednesday December 06, 2023 @09:11AM (#38544102)
Here's an example i have running on a pi with a temperature sensor. All data is received as json messages in mqtt, then stored as is in a postgres table. The view turns it into relational data.

If i just need the messages between certain temperatures i can speed this up by adding an index on the 'temperature' field in json.

    create or replace view bme680_v(ts, message, temperature, humidity, pressure, gas, iaq) as
    SELECT mqtt_raw.created_at                                          AS ts
     , mqtt_raw.message
     , (mqtt_raw.message::jsonb ->> 'temperature'::text)::numeric    AS temperature
     , (mqtt_raw.message::jsonb ->> 'humidity'::text)::numeric       AS humidity
     , (mqtt_raw.message::jsonb ->> 'pressure'::text)::numeric       AS pressure
     , (mqtt_raw.message::jsonb ->> 'gas_resistance'::text)::numeric AS gas
     , (mqtt_raw.message::jsonb ->> 'IAQ'::text)::numeric            AS iaq
    FROM mqtt_raw
    WHERE mqtt_raw.topic::text = 'pi/bme680'::text
    ORDER BY mqtt_raw.created_at DESC;
Re:JSONB Has Landed in SQLite by renegade-otter on Wednesday December 06, 2023 @08:39AM (#38543697)
Indeed. Those who use Postgres are already familiar with the difference.

Rule of thumb: if you are not sure, or do not have time for the nuances, just use JSONB.

Re:JSONB Has Landed in SQLite by btown on Wednesday December 06, 2023 @11:16AM (#38545878)
There’s a huge nuance worth mentioning: with JSONB you lose key ordering in objects.

This may be desired - it makes two effectively-equal objects have the same representation at rest!

But if you are storing human-written JSON - say, configs from an internal interface, where one might collocate a “__foo_comments” key above “foo” - their layout will be lost, and this may lead to someone visually seeing their changes scrambled on save.

Re:JSONB Has Landed in SQLite by ash on Wednesday December 06, 2023 @12:54PM (#38547254)
What about SQLite JSONB? Does it maintain order?

UPDATE: I think SQLite JSONB does maintain order. For example:

    select json(jsonb('{"a": 1, "b": 2, "c": 3, "d": 4}'));"
    -- {"a":1,"b":2,"c":3,"d":4}
And it does maintain order when adding new keys:

    select json(jsonb_insert(jsonb('{"a": 1, "b": 2, "c": 3, "d": 4}'), '$.e', 99));
    -- {"a":1,"b":2,"c":3,"d":4,"e":99}
(tested on https://codapi.org/sqlite/)
Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @11:31AM (#38546075)
JSON processors are not required nor expected to retain object key ordering from either input or object construction order.
Re:JSONB Has Landed in SQLite by fl0ki on Wednesday December 06, 2023 @12:38PM (#38547048)
That compounds really badly with another lack of specificity in JSON: parsers can treat duplicate keys in arbitrary ways, e.g. keep only the first or last value, combine values somehow, error out, etc.

When keys are not unique, their order can matter again, and can matter differently to different parsers.

Re:JSONB Has Landed in SQLite by btown on Wednesday December 06, 2023 @12:42PM (#38547103)
The question is whether people think of their database as a processor of data or a storage location for data, exactly as it was provided. Given that a lot of people use SQLite as the latter, it's a worthwhile caveat to make people aware of.

Additionally, Javascript environments (formalized in ES2015), your text editor, and your filesystem all guarantee that they'll preserve object key order when JSON is evaluated. It's not unreasonable that someone would expect this of their database!

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @06:26PM (#38550896)
> The question is whether people think of their database as a processor of data or a storage location for data

If you use the database's JSON type(s) and/or functions, then yes, your database is a JSON processor.

And, yes, your database is not a dumb store, not if it's an RDBMS. The whole point of it is that it's not a dumb store.

Re:JSONB Has Landed in SQLite by seanhunter on Wednesday December 06, 2023 @02:14PM (#38548232)
If you're storing it exactly as provided then surely until you know it's actually valid json you want a string, not a json/jsonb column.
Re:JSONB Has Landed in SQLite by ash on Wednesday December 06, 2023 @12:50PM (#38547211)
> Javascript environments ... guarantee that they'll preserve object key order when JSON is evaluated

Yes, but only for non-integer keys.

Re:JSONB Has Landed in SQLite by gmfawcett on Thursday December 07, 2023 @06:20PM (#38563175)
Is there such a thing as a non-string JSON key to be evaluated? https://www.json.org/ suggests not.
Re:JSONB Has Landed in SQLite by ash on Thursday December 14, 2023 @09:30AM (#38641692)
Yes, in the context of JSON handling by JavaScript. I meant string keys that can be interpreted as non-negative numbers. Those are traversed first, the rest of the keys are coming in insertion order. For example:

    > JSON.stringify({'one': 1, '3': 3, 'two': 2, '2': 2, 'three': 3, '1': 1})
    '{"1":1,"2":2,"3":3,"one":1,"two":2,"three":3}'
Re:JSONB Has Landed in SQLite by gmfawcett on Sunday December 17, 2023 @09:42AM (#38673157)
Thanks for explaining. That behaviour is... interesting!
Re:JSONB Has Landed in SQLite by ash on Wednesday December 06, 2023 @12:48PM (#38547177)
Yes, but it's not impossible to achieve it in practice.

For example, JavaScript JSON.stringify and JSON.parse have well-defined behaviour:

> Properties are visited using the same algorithm as Object.keys(), which has a well-defined order and is stable across implementations

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

> The traversal order, as of modern ECMAScript specification, is well-defined and consistent across implementations. Within each component of the prototype chain, all non-negative integer keys (those that can be array indices) will be traversed first in ascending order by value, then other string keys in ascending chronological order of property creation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Similarly, Python json module guarantees that "encoders and decoders preserve input and output order by default. Order is only lost if the underlying containers are unordered." Since 3.7 dict maintains insertion order.

https://docs.python.org/3/library/json.html

https://docs.python.org/3/library/stdtypes.html#dict

Yes, JS and Python behaviour are not the same for all cases, however, non-integer keys do maintain order across Python and JavaScript.

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @01:00PM (#38547337)
> Yes, but it's not impossible to achieve it in practice.

For any one implementation. But there's a very large number of implementations. You just can't count on object key order being preserved, so don't.

Re:JSONB Has Landed in SQLite by anamexis on Wednesday December 06, 2023 @01:50PM (#38547970)
You can count on object key order being preserved when you control the implementation. Which is why it's useful to know whether a given implementation preserves key order.
Re:JSONB Has Landed in SQLite by ash on Wednesday December 06, 2023 @01:24PM (#38547665)
There are situations where keeping order is useful. For example, human-editable json config files.

I also gave an example of two implementations that are compatible for a useful subset of keys. By the way, SQLite JSONB keeps object keys in insertion order, similar to Python: https://news.ycombinator.com/item?id=38547254

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @06:28PM (#38550908)
> There are situations where keeping order is useful. For example, human-editable json config files.

You might have to just normalize every time you want to edit that.

Re:JSONB Has Landed in SQLite by sgbeal on Wednesday December 06, 2023 @01:06PM (#38547427)
> There’s a huge nuance worth mentioning: with JSONB you lose key ordering in objects.

JSON never guarantees any ordering. From json.org:

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

If you depend on a given implementation doing so, you're depending on a quirk of that implementation, not on a JSON-defined behavior.

Re:JSONB Has Landed in SQLite by pie_flavor on Wednesday December 06, 2023 @01:37PM (#38547825)
Using a function to query a fact about a string does not imply that the string should change, regardless of what guarantees are present within the format that the string is in. If you expect to get out the same string you put in, then there's nothing implementation-quirk about that.
Re:JSONB Has Landed in SQLite by sgbeal on Wednesday December 06, 2023 @01:42PM (#38547887)
> Using a function to query a fact about a string does not imply that the string should change, ... then there's nothing implementation-quirk about that.

Fair enough when considering only the string value. My ill-expressed point was more about JSON as a data exchange format - it may pass through any number of JSON parser/storage implementations, any of which may use an arbitrary order for the keys.

Re:JSONB Has Landed in SQLite by ric2b on Thursday December 07, 2023 @05:50PM (#38562895)
You're not storing a string, you're storing a JSON object. If you want to store a string use TEXT or VARCHAR.
Re:JSONB Has Landed in SQLite by pie_flavor on Friday December 08, 2023 @05:09PM (#38575252)
That is what this post chain is about. The nuance between choosing to store your JSON as a string in a TEXT column or as JSONB in a BLOB column.
Re:JSONB Has Landed in SQLite by kevincox on Wednesday December 06, 2023 @11:03AM (#38545655)
Slight caveat. It seems that you should default to JSONB for all cases except for values directly returned in the query result. As IIUC you will get a JSONB blob back and will be responsible for parsing it on the client.

So use it for writes:

    UPDATE t SET col = jsonb_*(?)
Also use it for filters if applicable (although this seems like a niche use case, I can't actually think of an example).

But if returning values you probably want to use `json_*` functions

    SELECT json_extract(col1, '$.myProp') FROM t
Otherwise you will end up receiving the binary format.
Re:JSONB Has Landed in SQLite by yencabulator on Wednesday December 06, 2023 @07:35PM (#38551409)
Wait is it really true that if you use SQLite's JSONB format, `select my_json_column from foo` becomes unreadable to humans? That seems.. unacceptable. One would expect it to convert its internal format back to JSON for consumption.
Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @11:27AM (#38546020)
TFA doesn't say that this is _the_ JSONB of PostgreSQL fame, but I assume it must be. JSONB is a brilliant binary JSON format. What makes it brilliant is that arrays and objects (which when serialized are a sort of array) are encoded by having N-1 lengths of values then 1 offset to the Nth value, then N-1 lengths of values then... The idea is that lengths are going to be similar, while offsets never are, so using lengths makes JSONB more compressible, but offsets are needed to enable random access, so if you store mostly lengths and sometimes offsets you can get O(1)ish random access while still retaining compressibility.

Originally JSONB used only offsets, but that made it incompressible. That held up a PostgreSQL release for some time while they figured out what to do about it.

Re:JSONB Has Landed in SQLite by ks2048 on Wednesday December 06, 2023 @12:02PM (#38546495)
Link? My reading last time I looked at this was that the sqlite and postgres “jsonb” were different.
Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @12:13PM (#38546688)
This is the first time that SQLite3 is getting JSONB. Idk if it's the same as PG's. TFA doesn't say. I assume they are the same or similar because I seriously doubt that D.R. Hipp is unaware of PG's JSONB, but then for that reason I am surprised that he didn't say in TFA.
Re:JSONB Has Landed in SQLite by anarazel on Wednesday December 06, 2023 @12:29PM (#38546926)
https://sqlite.org/draft/jsonb.html references postgres:

> The "JSONB" name is inspired by PostgreSQL, but the on-disk format for SQLite's JSONB is not the same as PostgreSQL's. The two formats have the same name, but they have wildly different internal representations and are not in any way binary compatible.

Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @12:27PM (#38546891)
https://sqlite.org/draft/jsonb.html says they're NOT the same.
Re:JSONB Has Landed in SQLite by cryptonector on Wednesday December 06, 2023 @12:26PM (#38546886)
Ah, https://sqlite.org/draft/jsonb.html says:

> The "JSONB" name is inspired by PostgreSQL, but the on-disk format for SQLite's JSONB is not the same as PostgreSQL's. The two formats have the same name, but they have wildly different internal representations and are not in any way binary compatible.

Re:JSONB Has Landed in SQLite by masklinn on Wednesday December 06, 2023 @12:56PM (#38547294)
> TFA doesn't say that this is _the_ JSONB of PostgreSQL fame, but I assume it must be.

Definitely is not: Hipp states

> JSONB is also slightly smaller than text JSON in most cases (about 5% or 10% smaller)

Whereas in Postgres jsonb commonly takes 10~20% more space than json.