12.17.2 Functions That Create JSON Values (original) (raw)

12.17.2 Functions That Create JSON Values

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

mysql> SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME());  
+---------------------------------------------+  
| JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) |  
+---------------------------------------------+  
| [1, "abc", null, true, "11:30:24.000000"]   |  
+---------------------------------------------+  
mysql> SELECT JSON_OBJECT('id', 87, 'name', 'carrot');  
+-----------------------------------------+  
| JSON_OBJECT('id', 87, 'name', 'carrot') |  
+-----------------------------------------+  
| {"id": 87, "name": "carrot"}            |  
+-----------------------------------------+  
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 the JSON type usingCAST(value AS JSON); seeConverting between JSON and non-JSON values, for more information.

Two aggregate functions generating JSON values are available (MySQL 5.7.22 and later).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 12.19, “Aggregate Functions”.