JSON documents and functions
Store JSON in a column, extract and filter with -> and ->>, and use the JSON functions that are compared with real MySQL and MariaDB.
Raft DB is JSON-native: documents are its storage format. In SQL you reach them through the JSON column type, the operators ->, ->> and MEMBER OF, and the JSON functions. The statements below were checked against MySQL and MariaDB (operators; functions, run on the MariaDB dialect).
A JSON column#
DROP DATABASE IF EXISTS m9n;
CREATE DATABASE m9n;
USE m9n;
CREATE TABLE j (id INT PRIMARY KEY, d JSON);
INSERT INTO j VALUES (1, '{"a": 1, "b": "x", "c": [1, 2, 3], "n": {"k": "v"}}'), (2, '{"a": 2, "b": "y"}');A value is checked when it is stored: a text that is not valid JSON is refused with MySQL's error 3140 and MySQL's wording and position (The document is empty., Missing a comma or ']' after an array element., and others).
Operators#
SELECT id, d->'$.a', d->>'$.b', d->'$.b', d->'$.c[1]', d->>'$.n.k' FROM j;| id | d->'$.a' | d->>'$.b' | d->'$.b' | d->'$.c[1]' | d->>'$.n.k' |
|---|---|---|---|---|---|
| 1 | 1 | x | "x" | 2 | v |
| 2 | 2 | y | "y" | NULL | NULL |
-> returns JSON (a string keeps its quotes); ->> returns the unquoted text. A path that finds nothing gives NULL.
Use them in WHERE:
SELECT id FROM j WHERE d->>'$.b' = 'y';
SELECT id FROM j WHERE d->'$.a' = 2;
SELECT 2 MEMBER OF('[1, 2, 3]'), 5 MEMBER OF('[1, 2, 3]'), 'a' MEMBER OF('["a", "b"]');
SELECT id FROM j WHERE 3 MEMBER OF(d->'$.c');The first two return id 2; MEMBER OF answers 1, 0, 1 in the third statement and id 1 in the fourth.
Functions#
The function examples are written for the MariaDB dialect; the same functions exist in the MySQL dialect where MySQL has them.
SET @j = '{"a": 1, "b": [10, 20, {"c": "x"}], "d": {"e": null, "f": true}, "g": 1.5, "h": "str"}';
SELECT JSON_VALID(@j), JSON_VALID('{bad'), JSON_VALID('[1,2]');
SELECT JSON_TYPE('{}'), JSON_TYPE('[]'), JSON_TYPE('1'), JSON_TYPE('"a"'), JSON_TYPE('true'), JSON_TYPE('null');
SELECT JSON_LENGTH(@j), JSON_LENGTH(@j, '$.b'), JSON_KEYS(@j, '$.d');
SELECT JSON_EXTRACT(@j, '$.a'), JSON_EXTRACT(@j, '$.b[1]'), JSON_EXTRACT(@j, '$.b[2].c'), JSON_EXTRACT(@j, '$.b[last]');
SELECT JSON_VALUE(@j, '$.a'), JSON_VALUE(@j, '$.h'), JSON_QUERY(@j, '$.b');JSON_VALID(@j) is 1 and JSON_VALID('{bad') is 0. Paths support array indexes ($.b[1], $.b[last], $[1 to 2]), wildcards ($.b[*], $.d.*) and recursive descent ($**.c).
Build and change documents#
SELECT JSON_OBJECT('a', 1, 'b', 'x', 'c', NULL, 'd', 4), JSON_OBJECT(), JSON_ARRAY(1, 'a', NULL, 1.5), JSON_ARRAY();
SELECT JSON_SET(@j, '$.a', 100), JSON_SET('{"a": 1}', '$.b', 2);
SELECT JSON_INSERT('{"a": 1}', '$.a', 5, '$.b', 6), JSON_REPLACE('{"a": 1}', '$.a', 5, '$.b', 6);
SELECT JSON_REMOVE('{"a": 1, "b": 2}', '$.a'), JSON_REMOVE('[1,2,3]', '$[1]');
SELECT JSON_ARRAY_APPEND('[1, 2]', '$', 3), JSON_ARRAY_INSERT('[1, 3]', '$[1]', 2);
SELECT JSON_MERGE_PRESERVE('{"a": 1}', '{"b": 2}'), JSON_MERGE_PATCH('{"a": 1, "b": 2}', '{"a": null, "c": 3}');JSON_SET replaces or adds, JSON_INSERT only adds, JSON_REPLACE only replaces: JSON_INSERT('{"a": 1}', '$.a', 5, '$.b', 6) gives {"a": 1, "b": 6} and JSON_REPLACE('{"a": 1}', '$.a', 5, '$.b', 6) gives {"a": 5}.
Search#
SELECT JSON_CONTAINS('{"a": 1, "b": 2}', '{"a": 1}'), JSON_CONTAINS('[1, 2, 3]', '[1, 3]');
SELECT JSON_CONTAINS_PATH(@j, 'one', '$.a', '$.zz'), JSON_CONTAINS_PATH(@j, 'all', '$.a', '$.zz');
SELECT JSON_SEARCH('["abc", "def", "abc"]', 'one', 'abc'), JSON_SEARCH('["abc", "def", "abc"]', 'all', 'abc');
SELECT JSON_OVERLAPS('[1, 2]', '[2, 3]'), JSON_OVERLAPS('[1]', '[2]');Invalid input gives NULL or MySQL's error rather than a silent guess: SELECT JSON_EXTRACT('not json', '$.a') is in the script.