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
->,->>andMEMBER OF(see JSON operators).JSON_PRETTYandJSON_STORAGE_SIZEare 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 answerFUNCTION ... 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:
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#
| Function | What it does | Dialect |
|---|---|---|
JSON_ARRAY | Returns a JSON array of the arguments. | MySQL and MariaDB |
JSON_ARRAY_APPEND | Appends val to the array found at path. | MySQL and MariaDB |
JSON_ARRAY_INSERT | Inserts val into an array at the position path names ($[1]); the members after it move up. | MySQL and MariaDB |
JSON_ARRAY_INTERSECT | Returns the members of array1 that are also in array2. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_COMPACT | Returns the document written without spaces. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_CONTAINS | 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. | MySQL and MariaDB |
JSON_CONTAINS_PATH | Returns 1 if the document has data at any ('one') or all ('all') of the paths, 0 if not. | MySQL and MariaDB |
JSON_DEPTH | 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. | MySQL and MariaDB |
JSON_EQUALS | Returns 1 if the two documents are equal. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_EXISTS | Returns 1 if the document has data at path, 0 if not. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_EXTRACT | Returns the data at the paths. | MySQL and MariaDB |
JSON_INSERT | Adds val at each path that does not exist yet; paths that exist are left alone. | MySQL and MariaDB |
JSON_KEYS | Returns the keys of the object (at path) as an array; NULL if it is not an object. | MySQL and MariaDB |
JSON_KEY_VALUE | Returns 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_LENGTH | Returns the number of members of an array or an object, 1 for a scalar, NULL if path finds nothing. | MySQL and MariaDB |
JSON_LOOSE | Returns the document in the form that MariaDB writes with spaces after : and ,. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_MERGE | Synonym of JSON_MERGE_PRESERVE (deprecated in MySQL). | MySQL and MariaDB |
JSON_MERGE_PATCH | 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. | MySQL and MariaDB |
JSON_MERGE_PRESERVE | Merges 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_NORMALIZE | Returns the document in the standard written form. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_OBJECT | Returns a JSON object of the key and value pairs. | MySQL and MariaDB |
JSON_OBJECT_TO_ARRAY | Returns 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_OVERLAPS | 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. | MySQL and MariaDB |
JSON_QUERY | Returns the array or object at path; NULL for a scalar or nothing. | MariaDB only; the MySQL dialect answers FUNCTION ... is unknown |
JSON_QUOTE | Returns the string as a JSON string literal, with quotes and escapes. | MySQL and MariaDB |
JSON_REMOVE | Removes the data at each path. | MySQL and MariaDB |
JSON_REPLACE | Replaces the data at each path that exists; paths that do not exist are ignored. | MySQL and MariaDB |
JSON_SEARCH | 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). | MySQL and MariaDB |
JSON_SET | Sets the value at each path: replaces it if it exists, adds it if it does not. | MySQL and MariaDB |
JSON_TYPE | Returns the type of a JSON value: OBJECT, ARRAY, STRING, INTEGER, DOUBLE, BOOLEAN or NULL. | MySQL and MariaDB |
JSON_UNQUOTE | Removes the quotes of a JSON string and reads its escapes. | MySQL and MariaDB |
JSON_VALID | Returns 1 if val is valid JSON, 0 if not, NULL for NULL. | MySQL and MariaDB |
JSON_VALUE | Returns the scalar at path as text. | MySQL and MariaDB |
JSON_ARRAY#
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:
SELECT JSON_ARRAY(1, 'a', NULL, 1.5);Result: [1, "a", null, 1.5]
JSON_ARRAY_APPEND#
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:
SELECT JSON_ARRAY_APPEND('[1, 2]', '$', 3);Result: [1, 2, 3]
JSON_ARRAY_INSERT#
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:
SELECT JSON_ARRAY_INSERT('[1, 3]', '$[1]', 2);Result: [1, 2, 3]
JSON_ARRAY_INTERSECT#
MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.
JSON_ARRAY_INTERSECT(array1, array2)Returns the members of array1 that are also in array2.
Example:
SELECT JSON_ARRAY_INTERSECT('[1, 2, 3]', '[2, 3, 4]');Result: [2, 3]
JSON_COMPACT#
MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.
JSON_COMPACT(json_doc)Returns the document written without spaces.
Example:
SELECT JSON_COMPACT('{"a": [1, 2]}');Result: {"a":[1,2]}
JSON_CONTAINS#
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:
SELECT JSON_CONTAINS('[1, 2, 3]', '[1, 3]'), JSON_CONTAINS('[1, 2]', '5');Result: 1, 0
JSON_CONTAINS_PATH#
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:
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#
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:
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.
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:
SELECT JSON_EQUALS('{"a": 1, "b": 2}', '{"b": 2, "a": 1}');Result: 1
JSON_EXISTS#
MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.
JSON_EXISTS(json_doc, path)Returns 1 if the document has data at path, 0 if not.
Example:
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#
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:
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#
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:
SELECT JSON_INSERT('{"a": 1}', '$.a', 5, '$.b', 6);Result: {"a": 1, "b": 6}
JSON_KEYS#
JSON_KEYS(json_doc [, path])Returns the keys of the object (at path) as an array; NULL if it is not an object.
Example:
SELECT JSON_KEYS('{"a": 1, "b": 2}');Result: ["a", "b"]
JSON_KEY_VALUE#
MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.
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:
SELECT JSON_KEY_VALUE('{"a": 1}', '$');Result: [{"key": "a", "value": 1}]
JSON_LENGTH#
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:
SELECT JSON_LENGTH('[1, 2, {"a": 3}]');Result: 3
JSON_LOOSE#
MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.
JSON_LOOSE(json_doc)Returns the document in the form that MariaDB writes with spaces after : and ,.
Example:
SELECT JSON_LOOSE('{"a":[1,2]}');Result: {"a": [1, 2]}
JSON_MERGE#
JSON_MERGE(json_doc, json_doc [, json_doc] ...)Synonym of JSON_MERGE_PRESERVE (deprecated in MySQL).
Example:
SELECT JSON_MERGE('[1]', '[2]');Result: [1, 2]
JSON_MERGE_PATCH#
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:
SELECT JSON_MERGE_PATCH('{"a": 1, "b": 2}', '{"a": null, "c": 3}');Result: {"b": 2, "c": 3}
JSON_MERGE_PRESERVE#
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:
SELECT JSON_MERGE_PRESERVE('{"a": 1}', '{"a": 2}');Result: {"a": [1, 2]}
JSON_NORMALIZE#
MariaDB only; the MySQL dialect answers FUNCTION ... is unknown.
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:
SELECT JSON_NORMALIZE('{"a":[1,2]}');Result: {"a": [1, 2]}
JSON_OBJECT#
JSON_OBJECT([key, val [, key, val] ...])Returns a JSON object of the key and value pairs. A NULL value becomes null.
Example:
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.
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:
SELECT JSON_OBJECT_TO_ARRAY('{"a": 1}');Result: [["a", 1]]
JSON_OVERLAPS#
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:
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.
JSON_QUERY(json_doc, path)Returns the array or object at path; NULL for a scalar or nothing.
Example:
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#
JSON_QUOTE(string)Returns the string as a JSON string literal, with quotes and escapes.
Example:
SELECT JSON_QUOTE('a"b');Result: "a\"b"
JSON_REMOVE#
JSON_REMOVE(json_doc, path [, path] ...)Removes the data at each path. A path that does not exist is ignored.
Example:
SELECT JSON_REMOVE('{"a": 1, "b": 2}', '$.a');Result: {"b": 2}
JSON_REPLACE#
JSON_REPLACE(json_doc, path, val [, path, val] ...)Replaces the data at each path that exists; paths that do not exist are ignored.
Example:
SELECT JSON_REPLACE('{"a": 1}', '$.a', 5, '$.b', 6);Result: {"a": 5}
JSON_SEARCH#
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:
SELECT JSON_SEARCH('["abc", "def", "abc"]', 'one', 'abc'), JSON_SEARCH('["abc", "def", "abc"]', 'all', 'abc');Result: "$[0]", ["$[0]", "$[2]"]
JSON_SET#
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:
SELECT JSON_SET('{"a": 1}', '$.b', 2);Result: {"a": 1, "b": 2}
JSON_TYPE#
JSON_TYPE(json_val)Returns the type of a JSON value: OBJECT, ARRAY, STRING, INTEGER, DOUBLE, BOOLEAN or NULL.
Example:
SELECT JSON_TYPE('[]'), JSON_TYPE('1.5'), JSON_TYPE('"a"');Result: ARRAY, DOUBLE, STRING
JSON_UNQUOTE#
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:
SELECT JSON_UNQUOTE('"abc"'), JSON_UNQUOTE('abc');Result: abc, abc
JSON_VALID#
JSON_VALID(val)Returns 1 if val is valid JSON, 0 if not, NULL for NULL.
Example:
SELECT JSON_VALID('[1,2]'), JSON_VALID('{bad');Result: 1, 0
JSON_VALUE#
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:
SELECT JSON_VALUE('{"a": 1}', '$.a');Result: 1