Skip to main content
Converts arbitrary values to JSON values, without parsing any JSON strings in the input. Arrays and structs are converted recursively into JSON arrays and JSON objects, respectively. Scalar values other than BOOLEAN, INTEGER, BIGINT, REAL, DOUBLE, or TEXT are converted to TEXT using the regular CAST function.

Syntax

Parameters

Return Type

JSON
  • If <value> is NULL the return value is SQL NULL.
  • If <value> is an array or struct containing nested SQL NULL values, these nested SQL NULLs are converted to JSON nulls.

Examples

Example The following query converts a STRUCT value containing nested STRUCT, ARRAY, and scalar values to JSON. All of the scalar data types involved are natively representable in JSON.

Rows: 1Execution time: 6.28ms

Example The following query converts a STRUCT value containing nested STRUCT, ARRAY, and NULL values to JSON.

Rows: 1Execution time: 5.79ms

Example The following query converts a plain SQL NULL value to JSON. Since the value is not nested within an ARRAY or STRUCT, it remains a SQL NULL value.

Rows: 1Execution time: 5.49ms

Example The following query converts a TIMESTAMP value to JSON. Since TIMESTAMP values are not natively representable in JSON, they are converted to TEXT and represented as JSON strings.

Rows: 1Execution time: 5.40ms

Example The following query converts a TEXT value containing valid JSON data to JSON. Note that the return value simply consists of a single JSON string though, i.e. the TO_JSON function does not parse JSON data contained in strings.

Rows: 1Execution time: 5.13ms