JSON_ EXTRACT_ <type>
On this page
The JSON_ function extracts data from a JSON object or array using a path specification.NULL if the path does not match or if either argument is NULL.
SingleStore supports the following JSON_ functions:
-
JSON_extracts numeric values and is equivalent to theEXTRACT_ DOUBLE ::%JSON operator. -
JSON_extracts text and binary values and is equivalent to theEXTRACT_ STRING ::$JSON operator.The result collation is controlled by the json_engine variable.extract_ string_ collation Refer to Sync Variables Lists for more information. -
JSON_extracts any valid JSON value, includingEXTRACT_ JSON true,false, JSON maps, and JSON lists. -
JSON_extractsEXTRACT_ BIGINT BIGINTvalues.For other data types booleans ( trueandfalse) are converted to1and0, respectively, and floating-point values are rounded to the nearest integer (for example,1.becomes4 1, and1.becomes5 2).
The :: operator can be applied to columns of any scalar type (JSON, BLOB, INT, and so on).ROW expression or RECORD type, such as ROW('value1', 'value2')::field_.ROW is not a type; it is an expression that returns a RECORD type.:: operator to a RECORD or ARRAY value returns an error.:: operator also cannot be used on user-defined function (UDF) outputs.ROW expression, define the individual named columns in the query.
Note
Refer to Accessing Fields in a JSON Object for more information on JSON operators ::, ::$, and ::%.
Syntax
JSON_EXTRACT_<type>(<json>, <keypath> [, …]);
Arguments
-
json: a valid JSON object or the name of a JSON column. -
keypath: The path specification; a comma-separated list of object keys or zero-indexed array positions.
Return Value
When JSON_ is used with a JSON object or array literal and a path specification, the return values are as listed below.
-
SQL
NULLif value is JSON NULL (such as{"a": null}), or if keyname does not exist. -
The extracted element if
<json>is a valid JSON object or array and the path specified by the list of<keypath>s exists in<json>. -
SQL
NULLif<json>is not a valid JSON object or array or if the path specified by the list of<keypaths>does not exist in<json>. -
SQL
NULLif<json>is SQLNULL, an empty string, a JSON string, a JSON number, a JSON boolean value, or an empty JSON object or array. -
JSON
nullforJSON_if the value is JSONEXTRACT_ JSON null(such as{"a": null}). -
SQL
NULLforJSON_if the value is JSONEXTRACT_ STRING null(such as{"a": null}). -
0 for
JSON_andEXTRACT_ BIGINT JSON_if the value is JSONEXTRACT_ DOUBLE null(such as{"a": null}).
JSON_ EXTRACT_ <type> with Path Specification
The following table summarizes the functionality and return types of JSON_ when both a JSON object or array literal and a path specification are provided as arguments.
|
|
Function |
Return |
Comments |
|---|---|---|---|
|
JSON Object or Array Literal |
|
json or SQL |
Extracts the JSON value found at the path ( If the extracted object is a JSON string, quotes are not removed. Returns SQL |
|
|
string or SQL |
Extracts the JSON value found at the path ( If the extracted object is a JSON string, quotes are removed. Returns SQL | |
|
|
integer or 0 |
If the extracted value is a JSON string, extracts a valid numeric prefix and rounds to an Integer. Otherwise, returns 0. | |
|
|
double or 0 |
If the extracted value is a JSON string, extracts a valid numeric prefix. Otherwise, returns 0. |
JSON_ EXTRACT_ <type> with Path Specification Examples
Example 1 - JSON_ EXTRACT_ DOUBLE
Extracts the value for beta.
SELECT JSON_EXTRACT_DOUBLE('{"alpha":1, "beta":2, "gamma": [3,4,5]}', 'beta')AS get_beta;
+----------+
| get_beta |
+----------+
| 2 |
+----------+Example 2 - JSON_ EXTRACT_ DOUBLE
Extracts value for 1 from an array.
SELECT JSON_EXTRACT_DOUBLE('[3,4,5]', 1) AS get_result;
+------------+
| get_result |
+------------+
| 4 |
+------------+Note
Since JSON uses zero-indexed array positions, extracting with the path 0 will extract the value 3 and the path 1 will extract the value 4 as in the example above.
Example 3 - JSON_ EXTRACT_ JSON
Extracts the value for the path gamma.
SELECT JSON_EXTRACT_JSON('{"alpha":1, "beta":2, "gamma": [3,4,5]}', 'gamma')AS get_gamma;
+-----------+
| get_gamma |
+-----------+
| [3,4,5] |
+-----------+Example 4 - JSON_ EXTRACT_ JSON - Array value
Extracts the value for the path gamma..[3,4,5] which is 4 in this example.
SELECT JSON_EXTRACT_JSON('{"alpha":1, "beta":2, "gamma": [3,4,5]}', 'gamma', 1)AS get_gamma;
+-----------+
| get_gamma |
+-----------+
| 4 |
+-----------+Example 5 - JSON_ EXTRACT_ JSON - NULL value
The result in the following example is NULL as zeta has no value.
SELECT JSON_EXTRACT_JSON('{"alpha":1, "beta":2, "gamma": [3,4,5]}', 'zeta')AS get_zeta;
+-----------+
| get_zeta |
+-----------+
| NULL |
+-----------+Example 6 - JSON_ EXTRACT_ BIGINT
The result in the next example is rounded up to the next whole integer.1. the result would have been 1.
SELECT JSON_EXTRACT_BIGINT('{"alpha":1, "beta":2, "gamma": [3,4,5], "delta":1.5}', 'delta')AS get_delta;
+-----------+
| get_delta |
+-----------+
| 2 |
+-----------+Example 7 - JSON_ EXTRACT_ STRING
The value for the path gamma is an array so the entire array is returned as a string value.
SELECT JSON_EXTRACT_STRING('{"alpha":1, "beta":2, "gamma": [3,4,5]}', 'gamma')AS get_gamma;
+-----------+
| get_gamma |
+-----------+
| [3,4,5] |
+-----------+Example 8 - JSON_ EXTRACT_ STRING
Returns the value for the path gamma which is a non-numeric string in this example.
SELECT JSON_EXTRACT_STRING('{"alpha":1, "beta":2, "gamma": "A string"}', 'gamma')AS get_gamma;
+-----------+
| get_gamma |
+-----------+
| A string |
+-----------+Last modified: