Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/doc/json-enhancements.md # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / doc / json-enhancements.md` 144 lines ยท 7180 bytes Raw Blame History Rendered 1 # JSON Functions Enhancements (2022) 2 3 This document summaries enhancements to the SQLite JSON support added in 4 early 2022. 5 6 ## 1.0 Change summary: 7 8 1. New **->** and **->>** operators that work like MySQL and PostgreSQL (PG). 9 2. JSON functions are built-in rather than being an extension. They 10 are included by default, but can be omitted using the 11 -DSQLITE_OMIT_JSON compile-time option. 12 13 14 ## 2.0 New operators **->** and **->>** 15 16 The SQLite language adds two new binary operators **->** and **->>**. 17 Both operators are similar to json_extract(). The left operand is 18 JSON and the right operand is a JSON path expression (possibly abbreviated 19 for compatibility with PG - see below). So they are similar to a 20 two-argument call to json_extract(). 21 22 The difference between -> and ->> (and json_extract()) is as follows: 23 24 * The -> operator always returns JSON. 25 26 * The ->> operator converts the answer into a primitive SQL datatype 27 such as TEXT, INTEGER, REAL, or NULL. If a JSON object or array 28 is selected, that object or array is rendered as text. If a JSON 29 value is selected, that value is converted into its corresponding 30 SQL type 31 32 * The json_extract() interface returns JSON when a JSON object or 33 array is selected, or a primitive SQL datatype when a JSON value 34 is selected. This is different from MySQL, in which json_extract() 35 always returns JSON, but the difference is retained because it has 36 worked that way for 6 years and changing it now would likely break 37 a lot of legacy code. 38 39 In MySQL and PG, the ->> operator always returns TEXT (or NULL) and never 40 INTEGER or REAL. This is due to limitations in the type handling capabilities 41 of those systems. In MySQL and PG, the result type a function or operator 42 may only depend on the type of its arguments, never the value of its arguments. 43 But the underlying JSON type depends on the value of the JSON path 44 expression, not the type of the JSON path expression (which is always TEXT). 45 Hence, the result type of ->> in MySQL and PG is unable to vary according 46 to the type of the JSON value being extracted. 47 48 The type system in SQLite is more general. Functions in SQLite are able 49 to return different datatypes depending on the value of their arguments. 50 So the ->> operator in SQLite is able to return TEXT, INTEGER, REAL, or NULL 51 depending on the JSON type of the value being extracted. This means that 52 the behavior of the ->> is slightly different in SQLite versus MySQL and PG 53 in that it will sometimes return INTEGER and REAL values, depending on its 54 inputs. It is possible to implement the ->> operator in SQLite so that it 55 always operates exactly like MySQL and PG and always returns TEXT or NULL, 56 but I have been unable to think of any situations where returning the 57 actual JSON value this would cause problems, so I'm including the enhanced 58 functionality in SQLite. 59 60 The table below attempts to summarize the differences between the 61 -> and ->> operators and the json_extract() function, for SQLite, MySQL, 62 and PG. JSON values are shown using their SQL text representation but 63 in a bold font. 64 65 66
| JSON | PATH | -> operator (all) | ->> operator (MySQL/PG) 68 | ->> operator (SQLite) | json_extract() (SQLite) 69 |
|---|---|---|---|---|---|
| **'{"a":123}'** | '$.a' | **'123'** | '123' | 123 | 123 70 |
| **'{"a":4.5}'** | '$.a' | **'4.5'** | '4.5' | 4.5 | 4.5 71 |
| **'{"a":"xyz"}'** | '$.a' | **'"xyz"'** | 'xyz' | 'xyz' | 'xyz' 72 |
| **'{"a":null}'** | '$.a' | **'null'** | NULL | NULL | NULL 73 |
| **'{"a":[6,7,8]}'** | '$.a' | **'[6,7,8]'** | '[6,7,8]' | '[6,7,8]' | **'[6,7,8]'** 74 |
| **'{"a":{"x":9}}'** | '$.a' | **'{"x":9}'** | '{"x":9}' | '{"x":9}' | **'{"x":9}'** 75 |
| **'{"b":999}'** | '$.a' | NULL | NULL | NULL | NULL 76 |
| SQL expression | Works in MySQL? | Works in PG? | Works in SQLite 119 |
|---|---|---|---|
| value1->'$.a' | yes | no | yes 120 |
| value1->'a' | no | yes | yes 121 |
| value2->'$[2]' | yes | no | yes 122 |
| value2->2 | no | yes | yes 123 |