JSON data type
JSON data type
Section titled “JSON data type”The JSON data type stores JSON documents as defined by RFC 7159. Values are validated when they are stored and kept in a parsed form, so the JSON functions can access their parts without parsing the text again.
Creating JSON values
Section titled “Creating JSON values”A string assigned to a JSON column is parsed automatically. A string can also be converted with CAST or written as a JSON'…' literal, and JSON_OBJECT and JSON_ARRAY build JSON values from SQL values.
CREATE TABLE product_spec (product_id INT, spec JSON);INSERT INTO product_spec VALUES (1618, '{"weight_g": 280, "colors": ["black", "white"]}');SELECT product_id, spec, TYPEOF(spec) AS t, spec->>'$.colors[0]' AS first_color FROM product_spec;| product_id | spec | t | first_color |
|---|---|---|---|
| 1618 | {"weight_g":280,"colors":["black","white"]} | json | black |
SELECT CAST('{"a":1}' AS JSON) AS a, JSON'{"a":1}' AS b, TYPEOF(JSON'{"a":1}') AS t;| a | b | t |
|---|---|---|
| {"a":1} | {"a":1} | json |
JSON values are printed in compact form, without spaces between tokens.
JSON validation
Section titled “JSON validation”A string that is not valid JSON is rejected:
SELECT JSON'abc';ERROR: Invalid JSON: 'abc'.A JSON column can also be restricted with a JSON Schema (see Understanding JSON Schema for an introduction). Write the schema as a string after the type:
CREATE TABLE product_spec ( product_id INT, spec JSON ('{"type": "object", "required": ["weight_g"], "properties": {"weight_g": {"type": "integer"}, "colors": {"type": "array", "items": {"type": "string"}}}}'));
-- passes the schemaINSERT INTO product_spec VALUES (1618, '{"weight_g": 280, "colors": ["black", "white"]}');
-- "weight_g" is missingINSERT INTO product_spec VALUES (1619, '{"colors": ["red"]}');ERROR: The provided JSON has been invalidated by the JSON schema (Invalid schema path: #, Keyword: required, Invalid provided JSON path: #)SHOW COLUMNS shows the schema as part of the column type, for example JSON('{"type": "object", …}'). Casting a value to a JSON type with a schema also validates it.
JSON value types
Section titled “JSON value types”A JSON value is an object, an array, or a scalar (string, number, true, false or null), as defined by RFC 7159. JSON_TYPE returns the CoraDB name of the type:
| JSON value | CoraDB JSON type | Description |
|---|---|---|
| Object | JSON_OBJECT |
A set of key-value pairs |
| Array | JSON_ARRAY |
An ordered list of JSON values |
| string | STRING |
A quoted string |
| Number | INTEGER |
An integer that fits in 32 bits |
| Number | BIGINT |
An integer that fits in 64 bits |
| Number | DOUBLE |
Any other number: with a fraction, an exponent, or larger than 2^63 - 1 |
true, false |
BOOLEAN |
A boolean |
null |
JSON_NULL |
The JSON null value (not SQL NULL) |
SELECT JSON_TYPE(JSON'{"a":1}') AS a, JSON_TYPE(JSON'[1,2]') AS b, JSON_TYPE(JSON'"x"') AS c, JSON_TYPE(JSON'5') AS d, JSON_TYPE(JSON'5000000000') AS e, JSON_TYPE(JSON'5.5') AS f, JSON_TYPE(JSON'true') AS g, JSON_TYPE(JSON'null') AS h;| a | b | c | d | e | f | g | h |
|---|---|---|---|---|---|---|---|
| JSON_OBJECT | JSON_ARRAY | STRING | INTEGER | BIGINT | DOUBLE | BOOLEAN | JSON_NULL |
JSON data conversions
Section titled “JSON data conversions”To JSON. Values of other types are converted to JSON explicitly with CAST or implicitly when stored in a JSON column or passed to a JSON function:
| SQL type | Result |
|---|---|
| Any string type | Parsed as JSON text; the result can be of any JSON type |
| SHORT, INTEGER, BIGINT | INTEGER, or BIGINT if the value does not fit in 32 bits |
| FLOAT, DOUBLE, NUMERIC | DOUBLE |
From JSON.
| CoraDB JSON type | Can be converted to |
|---|---|
JSON_OBJECT, JSON_ARRAY |
A string with the JSON text |
STRING |
Any type a string can be converted to; as a string, without the quotes |
INTEGER, BIGINT, DOUBLE |
Any type the corresponding SQL number can be converted to |
BOOLEAN |
'true' or 'false' as a string; 1 or 0 as a number |
JSON_NULL |
The string 'null' |
SELECT CAST(JSON'true' AS STRING) AS a, CAST(JSON'true' AS INT) AS b, CAST(JSON'"42"' AS INT) AS c, CAST(JSON'"abc"' AS STRING) AS d, CAST(JSON'null' AS STRING) AS e, JSON_TYPE(CAST(5.25 AS JSON)) AS f;| a | b | c | d | e | f |
|---|---|---|---|---|---|
| true | 1 | 42 | abc | null | DOUBLE |
JSON paths
Section titled “JSON paths”Most JSON functions take a JSON path (or a JSON pointer, below) that addresses a part of a document. A path starts with $, the root, followed by any number of tokens:
<json_path> ::= $ [ <path_token> ] ...
<path_token> ::= [ index ] -- array element, from 0 | . key_identifier -- object member | . "key string" -- object member, any key | .* -- every member of an object | [*] -- every element of an array | ** <path_token> -- any sequence of keys and indexes, then the tokenIn {"a":[0,1,2,{"b":5}]}, the path $.a[3].b means "member b of element 3 of member a of the root" and addresses the value 5. A key that is not a valid identifier is written in double quotes, with JSON escapes: $."first name", or $."\"" for a key that is a double quote.
Paths with wildcards can match several values; functions such as JSON_EXTRACT then return them in an array:
SELECT JSON_EXTRACT('{"a":[0,1,2,{"b":5}]}', '$.a[3].b') AS a, JSON_EXTRACT('{"a":[0,1,2,{"b":5}]}', '$.a[*]') AS b, JSON_EXTRACT('{"a":{"x":1},"b":{"x":2}}', '$.*.x') AS c, JSON_EXTRACT('{"a":{"x":1},"b":[{"x":2}]}', '$**.x') AS d;| a | b | c | d |
|---|---|---|---|
| 5 | [0,1,2,{"b":5}] | [1,2] | [1,2] |
Paths are interpreted in UTF-8. A path in another character set is converted to UTF-8 first.
JSON pointers
Section titled “JSON pointers”A JSON pointer (RFC 6901) is an alternative way to write a path without wildcards. Each token is preceded by /, and an empty pointer is the root:
<json_pointer> ::= [ / path_token ] ... [ /- ]| JSON path | JSON pointer |
|---|---|
$.a[10].bb |
/a/10/bb |
$ |
(empty string) |
The special last token /- addresses the position after the last element of an array, for example to append a value: JSON_INSERT('[1,2]', '/-', 3) returns [1,2,3].
SELECT JSON_EXTRACT('{"a":[0,1,2,{"b":5}]}', '/a/3/b') AS by_pointer, JSON_EXTRACT('{"a":1}', '') AS root;| by_pointer | root |
|---|---|
| 5 | {"a":1} |
Like paths, pointers are interpreted in UTF-8.