You are viewing an older version of this section. View current production version.
JSON_EXTRACT_<type>
This is a set of three JSON functions, which returns the value of a given key in a JSON map or an element in a JSON array. Maps are in the form:
{"a": 1, "b": 2, "c": 3}
-
JSON_EXTRACT_DOUBLEis for all numeric data (equivalent to the::%JSON operator). JavaScript has only one numeric type. -
JSON_EXTRACT_STRINGis for all text and binary types (equivalent to the::$JSON operator). -
JSON_EXTRACT_JSONis for all valid JavaScript types, includingtrue,false, JSON maps, and lists.InfoFor more information on JSON operators
::,::$, and::%, see Accessing Fields in a JSON Object. Also, see the Character Encodings and Collation section for information on overriding collation settings.
JSON_EXTRACT_<type>(json, keypath)
Arguments
- json: a valid JSON map, or the name of a JSON column.
- keypath: The path to the key to extract (comma-separated list of dictionary keys or zero-indexed array positions).
Return Value
- The extracted element.
- SQL NULL if json is not a valid JSON array, or if keyname does not exist.
Examples
SELECT JSON_EXTRACT_DOUBLE('{"a":1, "b":2, "c": [3,4,5]}', 'b') AS get_b;
+-------+
| get_b |
+-------+
| 2 |
+-------+
SELECT JSON_EXTRACT_JSON('{"a":1, "b":2, "c": [3,4,5]}', 'c') AS get_c;
+---------+
| get_c |
+---------+
| [3,4,5] |
+---------+
SELECT JSON_EXTRACT_JSON('{"a":1, "b":2, "c": [3,4,5]}', 'c', 1) AS get_c;
+-------+
| get_c |
+-------+
| 4 |
+-------+
SELECT JSON_EXTRACT_JSON('{"a":1, "b":2, "c": [3,4,5]}', 'z') AS get_z;
+-------+
| get_z |
+-------+
| NULL |
+-------+
SELECT JSON_EXTRACT_STRING('{"a":1, "b":2, "c": [3,4,5]}', 'c') AS get_c;
+---------+
| get_c |
+---------+
| [3,4,5] |
+---------+
SELECT JSON_EXTRACT_STRING('{"a":1, "b":2, "c": "A string"}', 'c') AS get_c;
+----------+
| get_c |
+----------+
| A string |
+----------+