---
title: to_json function
description: Learn the syntax of the to\_json function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/to_json
---

# `to_json` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) 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`: A `STRUCT` expression, or a `VARIANT` in Databricks SQL and Databricks Runtime 15.3 and above.
* `options`: An optional `MAP` literal expression with keys and values being `STRING`. If `expr` is a `VARIANT`, the options are ignored.

## Returns

A `STRING`, or `BINARY` when `options` configures [Confluent Schema Registry](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_json.md#confluent-schema-registry).

For a complete list of options, see [JSON](https://docs.databricks.com/aws/en/spark/api-options.md#function-json).

## <a id="confluent-schema-registry" />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](https://docs.databricks.com/aws/en/structured-streaming/protocol-buffers.md#auth-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

```sql
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;
```

### Python

```python
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

```sql
> 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}
```

## Related functions

* [: operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/colonsign.md)
* [`from_csv` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/from_csv.md)
* [`schema_of_csv` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/schema_of_csv.md)
* [`from_json` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/from_json.md)
* [`schema_of_json` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/schema_of_json.md)
* [`parse_json` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/parse_json.md)
* [`to_xml` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_xml.md)
