JSON Functions
Functions for manipulating data in JSON.
DECODE_JSON(expression)
Example
Example 1. Encode a returned result set as a JSON string.
Query
select ENCODE_JSON(airline) as airline
from `travel-sample`.`inventory`.`airline` airline
where `id` = 10
Result
[
{
"airline": "{\"callsign\":\"MILE-AIR\",\"country\":\"United States\",\"iata\":\"Q5\",\"icao\":\"MLA\",\"id\":10,\"name\":\"40-Mile Air\",\"type\":\"airline\"}"
}
]
ENCODE_JSON(expression)
Example
Example 2. Unmarshal a JSON string into an SQL++ value.
Query
select DECODE_JSON("{\"airline\":{\"callsign\": \"Mile-Air\", \"country\": \"United States\", \"iata\": \"Q5\", \"id\": 10, \"name\": \"40-mile Air\", \"type\": \"airline\"}}") as jsonObj
Result
[
{
"jsonObj": {
"airline": {
"callsign": "Mile-Air",
"country": "United States",
"iata": "Q5",
"id": 10,
"name": "40-mile Air",
"type": "airline"
}
}
}
]
ENCODED_SIZE(expression)
POLY_LENGTH(expression)
Description
Returns length of the value after evaluating the expression. The exact meaning of length depends on the type of the value:
Example
Example 4. Return the length of the retrieved object.
Query
select POLY_LENGTH(airline) as airline_length
from `travel-sample`.`inventory`.`airline` airline
where `id` = 10
Result
[
{
"airline_length": 7
}
]
-
MISSING: MISSING
-
NULL: NULL
-
String: The length of the string.
-
Array: The number of elements in the array.
-
Object: The number of name/value pairs in the object
-
Any other value: NULL