JSON Functions
Overview
JSON functions are used to parse, query, construct, and validate JSON strings. AI DataLake supports JSON path expressions defined in the SQL standard (ISO/IEC TR 19075-6), and supports two modes: lax and strict. It provides complete JSON processing capabilities, including JSON validity determination, path query, scalar extraction, and JSON object and array construction.
For more information, see JSON Functions.
Application Scenarios
- JSON data validation: Uses IS JSON to determine whether a string is a valid JSON string, or whether it is a specific JSON type.
- JSON path query: Uses JSON_EXISTS to determine whether a path exists, uses JSON_VALUE to extract scalar values, and uses JSON_QUERY to extract JSON fragments.
- JSON serialization: Uses JSON_STRING to serialize values into JSON strings.
- JSON construction: Uses JSON_OBJECT and JSON_ARRAY to dynamically construct JSON objects and arrays.
Notes and Constraints
- The JSON path expression uses symbols to represent the root node. It supports attribute access (.a), array element access (.a[0].b), and array traversal (.a[*].b).
- There are two path modes: lax and strict. In lax mode, errors are more tolerant and are converted to empty sequences. In strict mode, data is checked from the schema perspective, and an error is thrown when data that does not comply with the path expression is encountered. If this parameter is omitted, the strict mode is used by default.
- JSON_VALUE can extract only scalar values. Non-scalar values cannot be returned. The following return types are supported: VARCHAR/STRING, BOOLEAN, INTEGER, and DOUBLE.
- The key of JSON_OBJECT must be a non-NULL string literal, and the value can be any expression.
- If ON NULL is omitted in JSON_ARRAY, ABSENT ON NULL (ignore NULL values) is used by default. If ON NULL is omitted in JSON_OBJECT, NULL ON NULL (include NULL as a NULL value) is used by default.
- Some functions in lax mode are not fully supported currently (upstream defect CALCITE-4717). Non-standard behaviors are not guaranteed.
Syntax Structure
| Syntax | Return Type | Description | Example |
|---|---|---|---|
| IS JSON [ { VALUE | SCALAR | ARRAY | OBJECT } ] | BOOLEAN | Determines whether a given string is valid JSON. The optional type parameter can be specified to restrict the allowed JSON object type. If the string is valid JSON but not of the specified type, false is returned. The default value is VALUE. | -- TRUE '1' IS JSON '[]' IS JSON '{}' IS JSON -- TRUE
'"abc"' IS JSON
-- FALSE
'abc' IS JSON
NULL IS JSON
-- TRUE
'1' IS JSON SCALAR
-- FALSE
'1' IS JSON ARRAY
-- FALSE
'1' IS JSON OBJECT
-- FALSE
'{}' IS JSON SCALAR
-- FALSE
'{}' IS JSON ARRAY
-- TRUE
'{}' IS JSON OBJECT |
| JSON_EXISTS(jsonValue, path [ { TRUE | FALSE | UNKNOWN | ERROR } ON ERROR ]) | BOOLEAN | Determines whether a JSON string satisfies a given path search condition. If the error handling behavior is omitted, the default value is FALSE ON ERROR. | -- TRUE
SELECT JSON_EXISTS('{"a": true}', '$.a');
-- FALSE
SELECT JSON_EXISTS('{"a": true}', '$.b');
-- TRUE
SELECT JSON_EXISTS('{"a": [{ "b": 1 }]}',
'$.a[0].b');
-- TRUE
SELECT JSON_EXISTS('{"a": true}', 'strict $.b' TRUE ON ERROR);
-- FALSE
SELECT JSON_EXISTS('{"a": true}', 'strict $.b' FALSE ON ERROR); |
| JSON_STRING(value) | STRING | Serializes a value to JSON. This function returns a JSON string containing the serialized value. If the value is NULL, the function returns NULL. | -- NULL JSON_STRING(CAST(NULL AS INT)) -- '1'
JSON_STRING(1)
-- 'true'
JSON_STRING(TRUE)
-- '"Hello, World!"'
JSON_STRING('Hello, World!')
-- '[1,2]'
JSON_STRING(ARRAY[1, 2]) |
| JSON_VALUE(jsonValue, path [RETURNING ] [ { NULL | ERROR | DEFAULT } ON EMPTY ] [ { NULL | ERROR | DEFAULT } ON ERROR ]) | STRING / BOOLEAN / INTEGER / DOUBLE | Extracts scalar values from JSON strings. This method searches for the given path expression in the JSON string and returns the value if the value at the path is a scalar. Non-scalar values cannot be returned. By default, the value is returned as a STRING. You can use dataType to select a different type. The supported types are VARCHAR/STRING, BOOLEAN, INTEGER, and DOUBLE. For empty path expressions or errors, you can define the behavior to return NULL, throw an error, or return a defined default value. If omitted, the default behavior is NULL ON EMPTY or NULL ON ERROR, respectively. The default value can be a literal or an expression. If the default value itself throws an error, the error behavior of ON EMPTY is used, and an error is thrown for ON ERROR. If the path contains special characters such as spaces, you can use ['property'] or ["property"] to select the specified property in the parent object. You must enclose the property name with single or double quotation marks. When JSON_VALUE is used in SQL, the path is a single quoted character parameter. Therefore, you need to escape the single quotation marks around the property name, for example, JSON_VALUE('{"a b": "true"}', '$.[''a b'']'). | -- "true" JSON_VALUE('{"a": true}', '$.a') -- TRUE
JSON_VALUE('{"a": true}', '$.a' RETURNING BOOLEAN)
-- "false"
JSON_VALUE('{"a": true}', 'lax $.b'
DEFAULT FALSE ON EMPTY)
-- "false"
JSON_VALUE('{"a": true}', 'strict $.b'
DEFAULT FALSE ON ERROR)
-- 0.998D
JSON_VALUE('{"a.b": [0.998,0.996]}','$.["a.b"][0]'
RETURNING DOUBLE)
-- "right"
JSON_VALUE('{"contains blank": "right"}', 'strict $.[''contains blank'']' NULL ON EMPTY DEFAULT 'wrong' ON ERROR) |
| JSON_QUERY(jsonValue, path [RETURNING ] [ { WITHOUT | WITH CONDITIONAL | WITH UNCONDITIONAL } [ ARRAY ] WRAPPER ] [ { NULL | EMPTY ARRAY | EMPTY OBJECT | ERROR } ON EMPTY ] [ { NULL | EMPTY ARRAY | EMPTY OBJECT | ERROR } ON ERROR ]) | STRING / ARRAY<STRING> | Extracts JSON values from a JSON string. The result is returned as a STRING or ARRAY<STRING> type. You can control the result using the RETURNING clause. The wrappingBehavior determines whether the extracted value should be wrapped as an array, and whether it should be wrapped unconditionally or only when the value itself is not an array. onEmpty and onError determine the behavior when the path expression is empty or an error occurs. By default, NULL is returned in both cases. Other options include using an empty array, an empty object, or throwing an error. | -- '{ "b": 1 }'
JSON_QUERY('{ "a": { "b": 1 } }', '$.a')
-- '[1, 2]'
JSON_QUERY('[1, 2]', '$')
-- NULL
JSON_QUERY(CAST(NULL AS STRING), '$')
-- '["c1","c2"]'
JSON_QUERY('{"a":[{"c":"c1"},{"c":"c2"}]}',
'lax $.a[*].c')
-- Wrap result into an array.
-- '[{}]'
JSON_QUERY('{}', '$' WITH CONDITIONAL ARRAY WRAPPER)
-- '[1, 2]'
JSON_QUERY('[1, 2]', '$' WITH CONDITIONAL ARRAY WRAPPER)
-- '[[1, 2]]'
JSON_QUERY('[1, 2]', '$' WITH UNCONDITIONAL ARRAY WRAPPER)
-- Scalars must be wrapped to return.
-- NULL
JSON_QUERY(1, '$')
-- '[1]'
JSON_QUERY(1, '$' WITH CONDITIONAL ARRAY WRAPPER)
-- Behavior if path expression is empty/there is an error.
-- '{}'
JSON_QUERY('{}', 'lax $.invalid' EMPTY OBJECT ON EMPTY)
-- '[]'
JSON_QUERY('{}', 'strict $.invalid' EMPTY ARRAY ON ERROR) |
| JSON_OBJECT([[KEY] key VALUE value]* [ { NULL | ABSENT } ON NULL ]) | STRING | Builds a JSON object string from a list of key-value pairs. Note that the key must be a non-NULL string literal, while the value can be any expression. This function returns a JSON string. The ON NULL behavior defines how NULL values are handled. If omitted, the default is NULL ON NULL. Values created from other JSON constructors (JSON_OBJECT, JSON_ARRAY) are inserted directly, not as strings. This allows for the construction of nested JSON structures. | -- '{}' JSON_OBJECT() -- '{"K1":"V1","K2":"V2"}'
JSON_OBJECT('K1' VALUE 'V1', 'K2' VALUE 'V2')
-- Expressions as values
JSON_OBJECT('orderNo' VALUE orders.orderId)
-- ON NULL
JSON_OBJECT(KEY 'K1' VALUE CAST(NULL AS STRING) NULL ON NULL) -- '{"K1":null}'
JSON_OBJECT(KEY 'K1' VALUE CAST(NULL AS STRING) ABSENT ON NULL) -- '{}'
-- '{"K1":{"K2":"V"}}'
JSON_OBJECT(
KEY 'K1'
VALUE JSON_OBJECT(
KEY 'K2'
VALUE 'V'
)
) |
| JSON_ARRAY([value]* [ { NULL | ABSENT } ON NULL ]) | STRING | Builds a JSON array string from a list of values. This function returns a JSON string. The value can be any expression. The ON NULL behavior defines how NULL values are handled. If omitted, the default is ABSENT ON NULL. Elements created from other JSON constructors (JSON_OBJECT, JSON_ARRAY) are inserted directly, not as strings. This allows for the construction of nested JSON structures. | -- '[]' JSON_ARRAY() -- '[1,"2"]' JSON_ARRAY(1, '2') -- Expressions as values JSON_ARRAY(orders.orderId) -- ON NULL JSON_ARRAY(CAST(NULL AS STRING) NULL ON NULL) -- '[null]' JSON_ARRAY(CAST(NULL AS STRING) ABSENT ON NULL) -- '[]' -- '[[1]]' JSON_ARRAY(JSON_ARRAY(1)) |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot