JSON Functions
- reference
Functions for encoding, decoding, and evaluating JSON values.
DECODE_JSON(expression)
Converts a JSON-encoded string into a SQL++ value.
Return Value
The function returns 1 of the following:
-
A SQL++ value.
-
NULLif the input is NULL or not a valid JSON value. -
MISSINGif the input is empty.
Example
SELECT DECODE_JSON(
"{\"airline\":
{ \"callsign\": \"Mile-Air\",
\"country\": \"United States\",
\"iata\": \"Q5\",
\"id\": 10,
\"name\": \"40-mile Air\",
\"type\": \"airline\"
}
}"
) as decoded_value;
[
{
"decoded_value": {
"airline": {
"callsign": "Mile-Air",
"country": "United States",
"iata": "Q5",
"id": 10,
"name": "40-mile Air",
"type": "airline"
}
}
}
]
ENCODE_JSON(expression)
Converts a SQL++ value into a JSON-encoded string.
Return Value
The function returns 1 of the following:
-
A JSON encoded string.
-
NULLif the input isNULL. -
MISSINGif the input isMISSING.
Example
SELECT ENCODE_JSON(
{
"airline": {
"callsign": "Mile-Air",
"country": "United States",
"iata": "Q5",
"id": 10,
"name": "40-mile Air",
"type": "airline"
}
}
) as encoded_value;
[
{
"encoded_value":
"{ \"airline\":
{ \"callsign\":\"Mile-Air\",
\"country\":\"United States\",
\"iata\":\"Q5\",
\"id\":10,
\"name\":\"40-mile Air\",
\"type\":\"airline\"
}
}"
}
]
ENCODED_SIZE(expression)
POLY_LENGTH(expression)
Evaluates an expression and returns the length of the resulting value. The definition of length depends on the type of the evaluated value. For more information, see the Return Value section.
Return Value
The function returns a value based on the data type of the result:
-
String: Returns the number of characters in the string.
-
Array: Returns the number of elements in the array.
-
Object: Returns the number of name/value pairs in the object.
-
MISSING: Returns
MISSING. -
NULL: Returns
NULL. -
Any other value: Returns
NULL.
Example
SELECT
POLY_LENGTH("Flight 101") as string_length,
POLY_LENGTH(["Flight 101", "Flight 202", "Flight 303"]) as array_length,
POLY_LENGTH({
"flight": 101,
"airline": "Mile-Air",
"destination": "United States"
}) as object_length;
[
{
"string_length": 10,
"array_length": 3,
"object_length": 3
}
]