Important
The SingleStore 9.1 release candidate (RC) gives you the opportunity to preview, evaluate, and provide feedback on new and upcoming features prior to their general availability. In the interim, SingleStore 9.0 is recommended for production workloads, which can later be upgraded to SingleStore 9.1.
JSON_ EXTRACT_ <type>
On this page
Extracts data from JSON, optionally based on a path specification.
When used with JSON object or array and a path specification, extracts data from the JSON object or array that matches the path specification.
When used with a JSON argument and no path specification, extracts data from the JSON to a value of <type>.
The following JSON_ functions are supported:
-
JSON_is for all numeric data (equivalent to theEXTRACT_ DOUBLE ::%JSON operator). -
JSON_is for all text and binary types (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_is for all valid JSON types, includingEXTRACT_ JSON true,false, JSON maps, and JSON lists. -
JSON_is for allEXTRACT_ BIGINT BIGINTtypes.Other data types are handled as follows: Booleans (true and false) become 1 and 0, respectively. Floats are rounded as follows: 1. 4 becomes 1, while 1. 5 becomes 2. For all other data types, 0 is returned. Note
For more information on JSON operators
::,::$, and::%, see Accessing Fields in a JSON Object.Also, see Character Encodings and Collation for information on overriding collation settings.
Syntax
JSON_EXTRACT_<type>(<json>, <keypath> [, ]);JSON_EXTRACT_<type>(<json>);
Arguments
-
json: valid JSON, or the name of a JSON column. -
keypath: (optional) 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}).
Refer to JSON_JSON_ when used with a JSON argument only.
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> - JSON Argument Only
The following table summarizes the functionality and return values of JSON_ over a JSON argument only.
For all functions, the return value matches the type of the function.JSON_ returns a JSON value, JSON_ returns a string value, and so on.
|
<json> |
Function |
Return |
Notes |
|---|---|---|---|
|
SQL |
|
SQL | |
|
Empty string |
|
SQL | |
|
JSON |
|
JSON |
Returns JSON |
|
|
SQL | ||
|
Empty JSON Object |
|
|
Returns JSON value. |
|
|
|
Returns string value. | |
|
|
|
Return value matches type of function. | |
|
JSON boolean value |
|
|
Returns JSON value. |
|
|
|
Returns string value. | |
|
|
|
Returns 1 for true, 0 for false. Return value matches type of function. | |
|
JSON string |
|
"<string>" |
Does not remove quotes. |
|
|
<string > |
Removes quotes. | |
|
|
numeric prefix or
|
If string has a valid numeric prefix, returns the numeric prefix, rounded appropriately. Returns 0 if the string does not have a valid numeric prefix. | |
|
JSON number |
|
numeric value |
Returns JSON value. |
|
|
numeric value |
Returns string value. | |
|
|
rounded numeric value |
Returns BIGINT. | |
|
|
numeric value |
Returns DOUBLE. | |
|
JSON object |
|
<json object> |
Extracts full object. |
|
|
0 |
Return value matches type of function. | |
|
JSON array |
|
<json array> |
Extracts full array. |
|
|
|
Return value matches type of function. | |
|
Invalid JSON |
|
SQL | |
|
|
|
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 |
+-----------+JSON_ EXTRACT_ <type> - JSON Argument Only Examples
Example 1 - JSON_ EXTRACT_ JSON
Extract from a JSON string with " " quotes.JSON_ function does not remove the " " quotes from the string.
SELECT JSON_EXTRACT_JSON('"a string"');
+---------------------------------------------+
| JSON_EXTRACT_JSON('"a string"') |
+---------------------------------------------+
| "a string" |
+---------------------------------------------+Extract from a string without " " quotes.
SELECT JSON_EXTRACT_JSON('a string');
+----------------------------------------+
| JSON_EXTRACT_JSON('a string') |
+----------------------------------------+
| NULL |
+----------------------------------------+The JSON_ function with one argument will return the JSON input if the input is valid JSON or SQL NULL if the input is invalid JSON.
Extract from a JSON array.
SELECT JSON_EXTRACT_JSON('["array element"]');
+----------------------------------------+
| JSON_EXTRACT_JSON('["array element"]') |
+----------------------------------------+
| ["array element"] |
+----------------------------------------+Extract from a JSON object with two key-value pairs, for which the values are arrays with a single element.
SELECT JSON_EXTRACT_JSON('{"key2":[2],"key3":[3]}');
+----------------------------------------------+
| JSON_EXTRACT_JSON('{"key2":[2],"key3":[3]}') |
+----------------------------------------------+
| {"key2":[2],"key3":[3]} |
+----------------------------------------------+Example 2 - JSON_ EXTRACT_ STRING
Extract from a string.JSON_ function removes the " " quotes from the string.
SELECT JSON_EXTRACT_STRING('"a string"');
+----------------------------------+
| JSON_EXTRACT_STRING('"a string"')|
+----------------------------------+
| a string |
+----------------------------------+The JSON_ function will return the JSON input as a string if the input is valid JSON or SQL NULL if the input is invalid JSON.
Extract from a JSON array with one element which is a string.
SELECT JSON_EXTRACT_STRING('["array element"]');
+------------------------------------------+
| JSON_EXTRACT_STRING('["array element"]') |
+------------------------------------------+
| ["array element"] |
+------------------------------------------+Extract from a JSON object with two key-value pairs, for which the values are arrays with a single element.
SELECT JSON_EXTRACT_STRING('{"key2":[2],"key3":[3]}');
+------------------------------------------------+
| JSON_EXTRACT_STRING('{"key2":[2],"key3":[3]}') |
+------------------------------------------------+
| {"key2":[2],"key3":[3]} |
+------------------------------------------------+Example 3 - JSON_ EXTRACT_ BIGINT
Extract from a JSON double value.
SELECT JSON_EXTRACT_BIGINT('3.14');
+-------------------------------+
| JSON_EXTRACT_BIGINT('3.14') |
+-------------------------------+
| 3 |
+-------------------------------+Extract from a JSON string with a numeric prefix.
SELECT JSON_EXTRACT_BIGINT('"12.3a"');
+-------------------------------+
| JSON_EXTRACT_BIGINT('"12.3a"')|
+-------------------------------+
| 12 |
+-------------------------------+Example 4 - JSON_ EXTRACT_ DOUBLE
Extract from a JSON double value.
SELECT JSON_EXTRACT_DOUBLE('3.14');
+-------------------------------+
| JSON_EXTRACT_DOUBLE('3.14') |
+-------------------------------+
| 3.14 |
+-------------------------------+Extract from a JSON string containing a double value.
SELECT JSON_EXTRACT_DOUBLE('"12.3a"');
+-------------------------------+
| JSON_EXTRACT_DOUBLE('"12.3a"')|
+-------------------------------+
| 12.3 |
+-------------------------------+Encoded Operations for JSON_ EXTRACT_ BIGINT
When you use JSON_ on a JSON columnstore column stored as SeekableJSON, SingleStore evaluates predicates and GROUP BY operations directly on the encoded representation of extracted integer or boolean values.GROUP BY, and aggregate functions over JSON integer and boolean key paths.
Encoded operations for JSON_ apply only when the following conditions are met:
-
The JSON column uses SeekableJSON encoding.
-
The key path contains non-repeating values (at most one value or
NULLper row).JSON arrays or nested collections at that path are not supported. -
The extracted values use one of the following internal encodings: Integer, IntegerRLE, or SeekableIntegerRLE.
-
The query accesses the key path using
JSON_.EXTRACT_ BIGINT(. . . ) -
The query does not also select the full JSON document from the same column.
For example, selecting the JSON column together with JSON_disables this optimization.EXTRACT_ BIGINT(. . . )
Note
Other JSON extraction functions, such as JSON_ and JSON_, decode JSON values before evaluation.
Example
The following example uses encoded operations for both filtering and aggregation:
-- Uses encoded operations for filter and aggregation
SELECT
JSON_EXTRACT_BIGINT(data, 'is_premium') AS is_premium,
JSON_EXTRACT_BIGINT(data, 'level') DIV 10 AS level_bucket,
COUNT(*) AS count,
AVG(JSON_EXTRACT_BIGINT(data, 'points')) AS avg_points
FROM json_mixed_test
WHERE JSON_EXTRACT_BIGINT(data, 'level') > 20
GROUP BY is_premium, level_bucket;In this example:
-
The
WHERE JSON_predicate runs on encoded integer values.EXTRACT_ BIGINT(data, 'level') > 20 -
The computed expression level
DIV 10operates on encoded data. -
GROUP BYandAVG(points)also execute on encoded values. -
The engine decodes only rows that satisfy the filter, rather than all rows in the table.
Last modified: March 2, 2026