value (usually shown as json_doc  or json_val  in the documentation for MySQL JSON functions), as the following examples demonstrate: 

•Attempting to insert a value into a JSON  column succeeds if the value is a valid JSON value, but fails if it is not: 

mysql> CREATE TABLE t1 (jdoc JSON); Query OK, 0 rows affected (0.20 sec) mysql> INSERT INTO t1 VALUES(’{"key1": "value1", "key2": "value2"}’); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO t1 VALUES(’[1, 2,’); ERROR 3140 (22032) at line 2: Invalid JSON text: "Invalid value." at position 6 in value (or column) ’[1, 2,’. 

Positions for “at position N ”  in such error messages are 0-based, but should be considered rough indications of where the problem in a value actually occurs. 

•The JSON_TYPE()  function expects a JSON argument and attempts to parse it into a JSON value. It returns the value’s JSON type if it is valid and produces an error otherwise: 

mysql> SELECT JSON_TYPE(’["a", "b", 1]’); +----------------------------+ | JSON_TYPE(’["a", "b", 1]’) | +----------------------------+ | ARRAY | +----------------------------+ mysql> SELECT JSON_TYPE(’"hello"’); +----------------------+ | JSON_TYPE(’"hello"’) | +----------------------+ | STRING | +----------------------+ mysql> SELECT JSON_TYPE(’hello’); ERROR 3146 (22032): Invalid data type for JSON data in argument 1 to function json_type; a JSON string or JSON type is required. 

MySQL handles strings used in JSON context using the utf8mb4  character set and utf8mb4_bin collation. Strings in other character sets are converted to utf8mb4  as necessary. (For strings in the ascii or utf8  character sets, no conversion is needed because ascii  and utf8  are subsets of utf8mb4 .) 

As an alternative to writing JSON values using literal strings, functions exist for composing JSON values from component elements. JSON_ARRAY()  takes a (possibly empty) list of values and returns a JSON array containing those values: 

mysql> SELECT JSON_ARRAY(’a’, 1, NOW()); +----------------------------------------+ | JSON_ARRAY(’a’, 1, NOW()) | +----------------------------------------+ | ["a", 1, "2015-07-27 09:43:47.000000"] | +----------------------------------------+ 

JSON_OBJECT()  takes a (possibly empty) list of key-value pairs and returns a JSON object containing those pairs: 

mysql> SELECT JSON_OBJECT(’key1’, 1, ’key2’, ’abc’); +---------------------------------------+ | JSON_OBJECT(’key1’, 1, ’key2’, ’abc’) | +---------------------------------------+ | {"key1": 1, "key2": "abc"} | 

