SQL reference (MySQL and MariaDB dialects)

JSON functions

Every JSON_ function of Raft DB, with the differences between the MySQL and MariaDB dialect and the JSON operators.

A JSON document is passed to a function as text. What another JSON function returned (or CAST(x AS JSON), or a JSON column value) is read as JSON; any other text is read as a plain string where a value is expected, and as a document where a document is expected.

Invalid documents#

A function that needs a document and gets text that is not JSON behaves as the dialect does: the MySQL dialect refuses it with error 3141 (The JSON text given as argument 1 of function ... is invalid), with MySQL's wording and position (Missing a name for object member., The document is empty., Missing a comma or ']' after an array element., ...). The MariaDB dialect answers NULL. Inserting invalid text into a JSON column is error 3140, with table.column named.

Output form#

Both servers write {"a": 1, "b": [1, 2]} (a space after : and ,); Raft DB does the same. MySQL's JSON column type stores a document in its own binary form and reorders the members of an object (shorter keys first, then alphabetically); Raft DB keeps a document as the text it was given, so the members stay in the order written.

The dialects#

  • MySQL only: the operators ->, ->> and MEMBER OF (see JSON operators). JSON_PRETTY and JSON_STORAGE_SIZE are not supported yet.
  • MariaDB only: JSON_QUERY, JSON_EXISTS, JSON_EQUALS, JSON_COMPACT, JSON_LOOSE, JSON_NORMALIZE, JSON_KEY_VALUE, JSON_OBJECT_TO_ARRAY, JSON_ARRAY_INTERSECT. In the MySQL dialect they answer FUNCTION ... is unknown (1305).
  • Different results: JSON_VALUE (see the entry), JSON_ARRAYAGG (MariaDB writes the array without spaces; see Aggregate functions).

Not supported yet: JSON_TABLE, JSON_OBJECTAGG, JSON_SCHEMA_VALID, JSON_DETAILED, JSON_PRETTY, JSON_STORAGE_SIZE, JSON_STORAGE_FREE.

The examples that use @j assume this document:

SQL
SET @j = '{"a": 1, "b": [10, 20, {"c": "x"}], "d": {"e": null, "f": true}, "g": 1.5, "h": "str"}';

The output is identical to MariaDB 11.4 and MySQL 9.7.

Summary of functions#

FunctionWhat it doesDialect
JSON_ARRAYReturns a JSON array of the arguments.MySQL and MariaDB
JSON_ARRAY_APPENDAppends val to the array found at path.MySQL and MariaDB
JSON_ARRAY_INSERTInserts val into an array at the position path names ($[1]); the members after it move up.MySQL and MariaDB
JSON_ARRAY_INTERSECTReturns the members of array1 that are also in array2.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_COMPACTReturns the document written without spaces.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_CONTAINSReturns 1 if the document candidate is contained in target (or in the part of it at path): a scalar is contained in an array that has it, an object in an object that has all its members with equal or containing values.MySQL and MariaDB
JSON_CONTAINS_PATHReturns 1 if the document has data at any ('one') or all ('all') of the paths, 0 if not.MySQL and MariaDB
JSON_DEPTHReturns the maximum depth of the document: 1 for a scalar, an empty array or an empty object, one more than the depth of the deepest member otherwise.MySQL and MariaDB
JSON_EQUALSReturns 1 if the two documents are equal.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_EXISTSReturns 1 if the document has data at path, 0 if not.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_EXTRACTReturns the data at the paths.MySQL and MariaDB
JSON_INSERTAdds val at each path that does not exist yet; paths that exist are left alone.MySQL and MariaDB
JSON_KEYSReturns the keys of the object (at path) as an array; NULL if it is not an object.MySQL and MariaDB
JSON_KEY_VALUEReturns the members of the object at path as an array of objects with the members key and value.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_LENGTHReturns the number of members of an array or an object, 1 for a scalar, NULL if path finds nothing.MySQL and MariaDB
JSON_LOOSEReturns the document in the form that MariaDB writes with spaces after : and ,.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_MERGESynonym of JSON_MERGE_PRESERVE (deprecated in MySQL).MySQL and MariaDB
JSON_MERGE_PATCHMerges documents as RFC 7396 says: a member of the second document replaces the member of the same key, a null removes it, objects merge recursively; anything else replaces the whole.MySQL and MariaDB
JSON_MERGE_PRESERVEMerges documents keeping all values: arrays are joined, objects are merged and values of the same key are collected in an array.MySQL and MariaDB
JSON_NORMALIZEReturns the document in the standard written form.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_OBJECTReturns a JSON object of the key and value pairs.MySQL and MariaDB
JSON_OBJECT_TO_ARRAYReturns an object as an array of [key, value] pairs; NULL if the document is not an object.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_OVERLAPSReturns 1 if the two documents have at least one member (arrays), one key with an equal value (objects) or the value itself (scalars) in common.MySQL and MariaDB
JSON_QUERYReturns the array or object at path; NULL for a scalar or nothing.MariaDB only; the MySQL dialect answers FUNCTION ... is unknown
JSON_QUOTEReturns the string as a JSON string literal, with quotes and escapes.MySQL and MariaDB
JSON_REMOVERemoves the data at each path.MySQL and MariaDB
JSON_REPLACEReplaces the data at each path that exists; paths that do not exist are ignored.MySQL and MariaDB
JSON_SEARCHReturns the path of the string value that matches search_str (a LIKE pattern with % and _): the first ('one') or all of them ('all', as an array).MySQL and MariaDB
JSON_SETSets the value at each path: replaces it if it exists, adds it if it does not.MySQL and MariaDB
JSON_TYPEReturns the type of a JSON value: OBJECT, ARRAY, STRING, INTEGER, DOUBLE, BOOLEAN or NULL.MySQL and MariaDB
JSON_UNQUOTERemoves the quotes of a JSON string and reads its escapes.MySQL and MariaDB
JSON_VALIDReturns 1 if val is valid JSON, 0 if not, NULL for NULL.MySQL and MariaDB
JSON_VALUEReturns the scalar at path as text.MySQL and MariaDB

JSON_ARRAY#

SQL
JSON_ARRAY([val [, val] ...])

Returns a JSON array of the arguments. A value that is the answer of another JSON function is inserted as JSON; any other text is a string.

Example:

SQL
SELECT JSON_ARRAY(1, 'a', NULL, 1.5);

Result: [1, "a", null, 1.5]

JSON_ARRAY_APPEND#

SQL
JSON_ARRAY_APPEND(json_doc, path, val [, path, val] ...)

Appends val to the array found at path. If the value at path is not an array it is first made the only member of one.

Example:

SQL
SELECT JSON_ARRAY_APPEND('[1, 2]', '$', 3);

Result: [1, 2, 3]

JSON_ARRAY_INSERT#

SQL
JSON_ARRAY_INSERT(json_doc, path, val [, path, val] ...)

Inserts val into an array at the position path names ($[1]); the members after it move up. A position beyond the end appends.

Example:

SQL
SELECT JSON_ARRAY_INSERT('[1, 3]', '$[1]', 2);

Result: [1, 2, 3]

JSON_ARRAY_INTERSECT#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_ARRAY_INTERSECT(array1, array2)

Returns the members of array1 that are also in array2.

Example:

SQL
SELECT JSON_ARRAY_INTERSECT('[1, 2, 3]', '[2, 3, 4]');

Result: [2, 3]

JSON_COMPACT#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_COMPACT(json_doc)

Returns the document written without spaces.

Example:

SQL
SELECT JSON_COMPACT('{"a": [1, 2]}');

Result: {"a":[1,2]}

JSON_CONTAINS#

SQL
JSON_CONTAINS(target, candidate [, path])

Returns 1 if the document candidate is contained in target (or in the part of it at path): a scalar is contained in an array that has it, an object in an object that has all its members with equal or containing values.

Example:

SQL
SELECT JSON_CONTAINS('[1, 2, 3]', '[1, 3]'), JSON_CONTAINS('[1, 2]', '5');

Result: 1, 0

JSON_CONTAINS_PATH#

SQL
JSON_CONTAINS_PATH(json_doc, one_or_all, path [, path] ...)

Returns 1 if the document has data at any ('one') or all ('all') of the paths, 0 if not.

Example:

SQL
SET @j = '{"a": 1, "b": [10, 20, {"c": "x"}], "d": {"e": null, "f": true}, "g": 1.5, "h": "str"}';
SELECT JSON_CONTAINS_PATH(@j, 'one', '$.a', '$.zz'), JSON_CONTAINS_PATH(@j, 'all', '$.a', '$.zz');

Result: 1, 0

JSON_DEPTH#

SQL
JSON_DEPTH(json_doc)

Returns the maximum depth of the document: 1 for a scalar, an empty array or an empty object, one more than the depth of the deepest member otherwise.

Example:

SQL
SELECT JSON_DEPTH('1'), JSON_DEPTH('[1]'), JSON_DEPTH('[[1]]');

Result: 1, 2, 3

JSON_EQUALS#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_EQUALS(json1, json2)

Returns 1 if the two documents are equal. The order of the members of an object does not matter; 1 and 1.0 are equal.

Example:

SQL
SELECT JSON_EQUALS('{"a": 1, "b": 2}', '{"b": 2, "a": 1}');

Result: 1

JSON_EXISTS#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_EXISTS(json_doc, path)

Returns 1 if the document has data at path, 0 if not.

Example:

SQL
SET @j = '{"a": 1, "b": [10, 20, {"c": "x"}], "d": {"e": null, "f": true}, "g": 1.5, "h": "str"}';
SELECT JSON_EXISTS(@j, '$.a'), JSON_EXISTS(@j, '$.nope');

Result: 1, 0

JSON_EXTRACT#

SQL
JSON_EXTRACT(json_doc, path [, path] ...)

Returns the data at the paths. One path that matches one value gives that value; several paths, or a path with a wildcard ([*], .*, **), give an array of the values. NULL if nothing matches. Paths understand .key, "quoted key", [n], [last], [last-n], [a to b], .*, [*] and **.

Example:

SQL
SET @j = '{"a": 1, "b": [10, 20, {"c": "x"}], "d": {"e": null, "f": true}, "g": 1.5, "h": "str"}';
SELECT JSON_EXTRACT(@j, '$.b[1]'), JSON_EXTRACT(@j, '$.b[last]'), JSON_EXTRACT(@j, '$.a', '$.h');

Result: 20, {"c": "x"}, [1, "str"]

JSON_INSERT#

SQL
JSON_INSERT(json_doc, path, val [, path, val] ...)

Adds val at each path that does not exist yet; paths that exist are left alone.

Example:

SQL
SELECT JSON_INSERT('{"a": 1}', '$.a', 5, '$.b', 6);

Result: {"a": 1, "b": 6}

JSON_KEYS#

SQL
JSON_KEYS(json_doc [, path])

Returns the keys of the object (at path) as an array; NULL if it is not an object.

Example:

SQL
SELECT JSON_KEYS('{"a": 1, "b": 2}');

Result: ["a", "b"]

JSON_KEY_VALUE#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_KEY_VALUE(json_doc, path)

Returns the members of the object at path as an array of objects with the members key and value.

Example:

SQL
SELECT JSON_KEY_VALUE('{"a": 1}', '$');

Result: [{"key": "a", "value": 1}]

JSON_LENGTH#

SQL
JSON_LENGTH(json_doc [, path])

Returns the number of members of an array or an object, 1 for a scalar, NULL if path finds nothing.

Example:

SQL
SELECT JSON_LENGTH('[1, 2, {"a": 3}]');

Result: 3

JSON_LOOSE#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_LOOSE(json_doc)

Returns the document in the form that MariaDB writes with spaces after : and ,.

Example:

SQL
SELECT JSON_LOOSE('{"a":[1,2]}');

Result: {"a": [1, 2]}

JSON_MERGE#

SQL
JSON_MERGE(json_doc, json_doc [, json_doc] ...)

Synonym of JSON_MERGE_PRESERVE (deprecated in MySQL).

Example:

SQL
SELECT JSON_MERGE('[1]', '[2]');

Result: [1, 2]

JSON_MERGE_PATCH#

SQL
JSON_MERGE_PATCH(json_doc, json_doc [, json_doc] ...)

Merges documents as RFC 7396 says: a member of the second document replaces the member of the same key, a null removes it, objects merge recursively; anything else replaces the whole.

Example:

SQL
SELECT JSON_MERGE_PATCH('{"a": 1, "b": 2}', '{"a": null, "c": 3}');

Result: {"b": 2, "c": 3}

JSON_MERGE_PRESERVE#

SQL
JSON_MERGE_PRESERVE(json_doc, json_doc [, json_doc] ...)

Merges documents keeping all values: arrays are joined, objects are merged and values of the same key are collected in an array.

Example:

SQL
SELECT JSON_MERGE_PRESERVE('{"a": 1}', '{"a": 2}');

Result: {"a": [1, 2]}

JSON_NORMALIZE#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_NORMALIZE(json_doc)

Returns the document in the standard written form. Raft DB writes it as JSON_LOOSE does and does not sort the members as MariaDB does.

Example:

SQL
SELECT JSON_NORMALIZE('{"a":[1,2]}');

Result: {"a": [1, 2]}

JSON_OBJECT#

SQL
JSON_OBJECT([key, val [, key, val] ...])

Returns a JSON object of the key and value pairs. A NULL value becomes null.

Example:

SQL
SELECT JSON_OBJECT('a', 1, 'b', 'x', 'c', NULL);

Result: {"a": 1, "b": "x", "c": null}

JSON_OBJECT_TO_ARRAY#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_OBJECT_TO_ARRAY(json_doc)

Returns an object as an array of [key, value] pairs; NULL if the document is not an object.

Example:

SQL
SELECT JSON_OBJECT_TO_ARRAY('{"a": 1}');

Result: [["a", 1]]

JSON_OVERLAPS#

SQL
JSON_OVERLAPS(json_doc1, json_doc2)

Returns 1 if the two documents have at least one member (arrays), one key with an equal value (objects) or the value itself (scalars) in common.

Example:

SQL
SELECT JSON_OVERLAPS('[1, 2]', '[2, 3]'), JSON_OVERLAPS('[1]', '[2]');

Result: 1, 0

JSON_QUERY#

MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.

SQL
JSON_QUERY(json_doc, path)

Returns the array or object at path; NULL for a scalar or nothing.

Example:

SQL
SET @j = '{"a": 1, "b": [10, 20, {"c": "x"}], "d": {"e": null, "f": true}, "g": 1.5, "h": "str"}';
SELECT JSON_QUERY(@j, '$.b'), JSON_QUERY(@j, '$.a');

Result: [10, 20, {"c": "x"}], NULL

JSON_QUOTE#

SQL
JSON_QUOTE(string)

Returns the string as a JSON string literal, with quotes and escapes.

Example:

SQL
SELECT JSON_QUOTE('a"b');

Result: "a\"b"

JSON_REMOVE#

SQL
JSON_REMOVE(json_doc, path [, path] ...)

Removes the data at each path. A path that does not exist is ignored.

Example:

SQL
SELECT JSON_REMOVE('{"a": 1, "b": 2}', '$.a');

Result: {"b": 2}

JSON_REPLACE#

SQL
JSON_REPLACE(json_doc, path, val [, path, val] ...)

Replaces the data at each path that exists; paths that do not exist are ignored.

Example:

SQL
SELECT JSON_REPLACE('{"a": 1}', '$.a', 5, '$.b', 6);

Result: {"a": 5}

JSON_SEARCH#

SQL
JSON_SEARCH(json_doc, one_or_all, search_str)

Returns the path of the string value that matches search_str (a LIKE pattern with % and _): the first ('one') or all of them ('all', as an array). NULL if none.

Example:

SQL
SELECT JSON_SEARCH('["abc", "def", "abc"]', 'one', 'abc'), JSON_SEARCH('["abc", "def", "abc"]', 'all', 'abc');

Result: "$[0]", ["$[0]", "$[2]"]

JSON_SET#

SQL
JSON_SET(json_doc, path, val [, path, val] ...)

Sets the value at each path: replaces it if it exists, adds it if it does not.

Example:

SQL
SELECT JSON_SET('{"a": 1}', '$.b', 2);

Result: {"a": 1, "b": 2}

JSON_TYPE#

SQL
JSON_TYPE(json_val)

Returns the type of a JSON value: OBJECT, ARRAY, STRING, INTEGER, DOUBLE, BOOLEAN or NULL.

Example:

SQL
SELECT JSON_TYPE('[]'), JSON_TYPE('1.5'), JSON_TYPE('"a"');

Result: ARRAY, DOUBLE, STRING

JSON_UNQUOTE#

SQL
JSON_UNQUOTE(json_val)

Removes the quotes of a JSON string and reads its escapes. Text that is not a quoted string is returned as it is.

Example:

SQL
SELECT JSON_UNQUOTE('"abc"'), JSON_UNQUOTE('abc');

Result: abc, abc

JSON_VALID#

SQL
JSON_VALID(val)

Returns 1 if val is valid JSON, 0 if not, NULL for NULL.

Example:

SQL
SELECT JSON_VALID('[1,2]'), JSON_VALID('{bad');

Result: 1, 0

JSON_VALUE#

SQL
JSON_VALUE(json_doc, path)

Returns the scalar at path as text. For an array or an object MySQL returns its text and MariaDB returns NULL; for true MySQL returns true and MariaDB 1.

Example:

SQL
SELECT JSON_VALUE('{"a": 1}', '$.a');

Result: 1

See also#