Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 43.3Mb
PDF (A4) - 43.4Mb
Man Pages (TGZ) - 297.1Kb
Man Pages (Zip) - 402.3Kb
Info (Gzip) - 4.3Mb
Info (Zip) - 4.3Mb
Excerpts from this Manual

MySQL 8.0 Reference Manual  / ...  / Functions and Operators  / JSON Functions  /  Functions That Create JSON Values

14.17.2 Functions That Create JSON Values

The functions listed in this section compose JSON values from component elements.

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

    Evaluates a (possibly empty) list of values and returns a JSON array containing those values.

    mysql> SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME());+---------------------------------------------+| JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) |+---------------------------------------------+| [1, "abc", null, true, "11:30:24.000000"]   |+---------------------------------------------+
  • JSON_OBJECT([key,val[,key,val] ...])

    Evaluates a (possibly empty) list of key-value pairs and returns a JSON object containing those pairs. An error occurs if any key name isNULL or the number of arguments is odd.

    mysql> SELECT JSON_OBJECT('id', 87, 'name', 'carrot');+-----------------------------------------+| JSON_OBJECT('id', 87, 'name', 'carrot') |+-----------------------------------------+| {"id": 87, "name": "carrot"}            |+-----------------------------------------+
  • JSON_QUOTE(string)

    Quotes a string as a JSON value by wrapping it with double quote characters and escaping interior quote and other characters, then returning the result as autf8mb4 string. ReturnsNULL if the argument isNULL.

    This function is typically used to produce a valid JSON string literal for inclusion within a JSON document.

    Certain special characters are escaped with backslashes per the escape sequences shown inTable 14.23, “JSON_UNQUOTE() Special Character Escape Sequences”.

    mysql> SELECT JSON_QUOTE('null'), JSON_QUOTE('"null"');+--------------------+----------------------+| JSON_QUOTE('null') | JSON_QUOTE('"null"') |+--------------------+----------------------+| "null"             | "\"null\""           |+--------------------+----------------------+mysql> SELECT JSON_QUOTE('[1, 2, 3]');+-------------------------+| JSON_QUOTE('[1, 2, 3]') |+-------------------------+| "[1, 2, 3]"             |+-------------------------+

You can also obtain JSON values by casting values of other types to theJSON type usingCAST(value AS JSON); seeConverting between JSON and non-JSON values, for more information.

Two aggregate functions generating JSON values are available.JSON_ARRAYAGG() returns a result set as a single JSON array, andJSON_OBJECTAGG() returns a result set as a single JSON object. For more information, seeSection 14.19, “Aggregate Functions”.