to_json function
Applies to: Databricks SQL
Databricks Runtime
Returns a JSON string with the STRUCT or VARIANT specified in expr, or binary data when using Confluent Schema Registry.
Syntax
to_json(expr [, options] )
Arguments
expr: ASTRUCTexpression, or aVARIANTin Databricks SQL and Databricks Runtime 15.3 and above.options: An optionalMAPliteral expression with keys and values beingSTRING. Ifexpris aVARIANT, the options are ignored.
Returns
A STRING, or BINARY when options configures Confluent Schema Registry.
For a complete list of options, see JSON.
Confluent Schema Registry
Confluent Schema Registry integration is supported in Databricks Runtime 19 and above on classic compute. Serverless compute is not supported.
Provide both schema.registry.subject and schema.registry.address in options:
schema.registry.subject(required): The subject containing the registered JSON schema.schema.registry.address(required): The URL of the Confluent Schema Registry.
The subject must already contain a JSON object schema. The input must be a STRUCT compatible with the subject's latest schema, including its required fields.
The binary output starts with a five-byte Confluent header: the magic byte 0x00, followed by a four-byte schema ID in big-endian order. The remaining bytes contain the JSON payload encoded in UTF-8.
Only Confluent Schema Registry is supported. Pass authentication and TLS settings as confluent.schema.registry.<option> entries. See Authenticate to an external Confluent Schema Registry.
The following examples assume that the events-value subject has a registered JSON object schema with an integer field named a and a number field named b.
- SQL
- Python
SELECT to_json(
named_struct('a', CAST(1 AS BIGINT), 'b', CAST(0.8 AS DOUBLE)),
map(
'schema.registry.subject', 'events-value',
'schema.registry.address', 'https://schema-registry.example.com'
)
) AS value;
from pyspark.sql import functions as F
registry_options = {
"schema.registry.subject": "events-value",
"schema.registry.address": "https://schema-registry.example.com",
}
record = F.struct(
F.lit(1).cast("bigint").alias("a"),
F.lit(0.8).alias("b"),
)
messages = spark.range(1).select(F.to_json(record, registry_options).alias("value"))
Common error conditions
to_json raises SCHEMA_REGISTRY_CONFIGURATION_ERROR if the registry configuration is invalid or the input struct is incompatible with the registered JSON schema.
Examples
> SELECT to_json(named_struct('a', 1, 'b', 2));
{"a":1,"b":2}
> SELECT to_json(named_struct('time', to_timestamp('2015-08-26', 'yyyy-MM-dd')), map('timestampFormat', 'dd/MM/yyyy'));
{"time":"26/08/2015"}
> SELECT to_json(array(named_struct('a', 1, 'b', 2)));
[{"a":1,"b":2}]
> SELECT to_json(map('a', named_struct('b', 1)));
{"a":{"b":1}}
> SELECT to_json(map(named_struct('a', 1),named_struct('b', 2)));
{"[1]":{"b":2}}
> SELECT to_json(map('a', 1));
{"a":1}
> SELECT to_json(array((map('a', 1))));
[{"a":1}]
-- VARIANT input
> SELECT to_json(parse_json('{"key": 123, "data": [4, 5, "str"]}'))
{"data":[4,5,"str"],"key":123}