TO_JSON

functionsupportedCore SQL functions

docs.snowflake.com ↗

Conversion functionsSemi-structured and structured data functions

Converts a VARIANT value to a string containing the JSON representation of the value.

Syntax

TO_JSON( <expr> )

Parameters

NameRequiredDescription
exprrequiredAn expression of type VARIANT that holds valid JSON information.

Returns

Returns a value of type VARCHAR. If the input is NULL, the function returns NULL.

Usage notes

Handling NULL values with the PARSE_JSON and TO_JSON functions

The following example shows how PARSE_JSON and TO_JSON handle NULL values:

Comparing PARSE_JSON and TO_JSON

The following examples demonstrate the relationship between the PARSE_JSON and TO_JSON functions. This example creates a table with a VARCHAR column and a VARIANT column. The INSERT statement inserts a VARCHAR value, and the UPDATE statement generates a JSON value that corresponds with that VARCHAR value. This query shows that TO_JSON and PARSE_JSON are conceptually reciprocal functions: However, the functions are not exactly reciprocal. Differences in whitespace or in the order of key-value pairs can prevent the output from matching the input. For example: