JSON_VALUE
Purpose
This function extracts a single SQL value at a given path from a valid JSON string.
Syntax
json_value::=
json_input_clause::=
Usage Notes
- The parameter
json_exprdefines the input JSON string for the function. Exasol accepts an arbitrary expression of type(VAR)CHAR. To return an SQL value,json_exprhas to follow the syntax rules on JSON.org. - The parameter
json_path_exprdefines the JSON path for the function. Exasol accepts an arbitrary expression of type(VAR)CHAR. For the information on path syntax rules in Exasol, refer to the JSON Path Expressions section. - The optional
RETURNINGdata_typeclause allows the specification of the SQL return type. The default type isVARCHAR(2000000). - If
JSON_VALUEcontainsERROR ON EMPTY, it throws an error if the path does not identify an SQL value. - If
JSON_VALUEcontainsNULL ON EMPTY, it returnsNULLif the path does not identify an SQL value. This is the default behavior. - If
JSON_VALUEcontainsDEFAULT expr ON EMPTY, it returnsexprif the path does not identify an SQL value. - If
JSON_VALUEcontainsERROR ON ERROR, it throws an error if an error occurs during the JSON processing. - If
JSON_VALUEcontainsNULL ON ERROR, it returnsNULLif an error occurs during the JSON processing. This is the default behavior. - If
JSON_VALUEcontainsDEFAULT expr ON ERROR, it returnsexprif an error occurs during the JSON processing. - To learn more about the different possible errors, refer to the JSON Error Handling section.
Example
CREATE OR REPLACE TABLE json_input(id INT, json VARCHAR(2000000));
INSERT INTO json_input VALUES(1, '{"name" : "Smith"}');
INSERT INTO json_input VALUES(2, '{"surname" : "Smith"}');
INSERT INTO json_input VALUES(3, '{"name" : 1');
INSERT INTO json_input VALUES(4, '{"name" : "Doe"}');
SELECT id,
JSON_VALUE(json, '$.name' NULL ON EMPTY DEFAULT 'invalid name' ON ERROR) as "JSON value"
FROM json_input;
Result
| ID | JSON value |
| 1 | Smith |
| 2 | NULL |
| 3 | invalid name |
| 4 | Doe |