

# JSON to SQL type conversion functions

Use these functions to convert JSON to SQL values, such as a `STRUCT` or a list, of the types that a structure describes.

Other functions on this page return the structure of one JSON value or of the values in a group, in the form that the conversion functions take.

## `json_group_structure` function
<a name="athena-duckdb-functions-json-json-group-structure"></a>

Returns one JSON value that names the keys and types found in the values of `json` across the group. An object is described by its keys, each with the type of its value, and an array is described by the type of its elements. Types are given as DuckDB type names, such as `UBIGINT`, `DOUBLE`, `VARCHAR`, and `BOOLEAN`.

When the values differ, the function combines them. The combined object has every key that appears in any value. A key that holds both integers and decimal numbers is described as `DOUBLE`. A key whose types cannot be combined, such as a number and a string, is described as `JSON`. JSON `null` values and `NULL` rows do not change the structure. If the group has no rows, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-json-json-group-structure-syntax"></a>

```sql
json_group_structure({{json}})
```

### Arguments
<a name="athena-duckdb-functions-json-json-group-structure-arguments"></a>

The `json_group_structure` function takes the following argument:

**{{json}}**  
The values to describe, usually a `JSON` column. Values of other types are converted with `to_json` first.

### Return type
<a name="athena-duckdb-functions-json-json-group-structure-return-type"></a>

`JSON`

### Examples
<a name="athena-duckdb-functions-json-json-group-structure-examples"></a>

The following SQL query combines the structure of two JSON objects. The key `a` holds an integer in one object and a decimal number in the other, so its type is `DOUBLE`.

```sql
SELECT
    json_group_structure(
        CAST(doc AS JSON)
    ) AS result FROM (VALUES
        ('{"a": 1, "b": "x"}'),
        ('{"a": 2.5, "c": [true]}')
    ) AS v(doc)
```

The query returns the following result.


| result | 
| --- | 
| `{"a":"DOUBLE","b":"VARCHAR","c":["BOOLEAN"]}` | 

The following SQL query has a key that holds a number in one object and a string in the other. The types cannot be combined, so the key is described as `JSON`.

```sql
SELECT
    json_group_structure(
        CAST(doc AS JSON)
    ) AS result FROM (VALUES
        ('{"id": 1}'),
        ('{"id": "A-7"}')
    ) AS v(doc)
```

The query returns the following result.


| result | 
| --- | 
| `{"id":"JSON"}` | 

The following SQL query passes a `VARCHAR` column without a cast. The function describes the text as a string, not as a JSON object.

```sql
SELECT
    json_group_structure(doc) AS result FROM (VALUES
        ('{"a": 1}')
    ) AS v(doc)
```

The query returns the following result.


| result | 
| --- | 
| `"VARCHAR"` | 

### Considerations
<a name="athena-duckdb-functions-json-json-group-structure-considerations"></a>

A `VARCHAR` value is described as a string, even when it holds JSON text, so the result is `"VARCHAR"`. Cast the column to `JSON` first, as in `json_group_structure(CAST({{column}} AS JSON))`.

Integers that are all zero or greater are described as `UBIGINT`, and integers that are all negative are described as `BIGINT`. When both occur, the combined type is `HUGEINT`.

`json_group_structure` is a macro, not a built-in aggregate function, so a call with `DISTINCT`, `ORDER BY`, or `FILTER` fails.

## `json_structure` function
<a name="athena-duckdb-functions-json-json-structure"></a>

Returns a JSON value with the same shape as `json`, in which each scalar is replaced by a type name. Each type name is the SQL type that the scalar converts to, such as `"UBIGINT"`, `"DOUBLE"`, `"VARCHAR"`, or `"BOOLEAN"`. JSON `null` becomes `"NULL"`. An array becomes an array with one element that describes all of its elements, and the members of the objects in an array are merged into one object.

When the elements of an array have types that do not combine, such as a number and a string, the element type is `"JSON"`. Integers and numbers with a fraction combine to `"DOUBLE"`. The result has the format that `json_transform` takes as its `structure` argument, so you can use a sample document to build the structure. If `json` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-json-json-structure-syntax"></a>

```sql
json_structure({{json}})
```

### Arguments
<a name="athena-duckdb-functions-json-json-structure-arguments"></a>

The `json_structure` function takes the following argument:

**{{json}}**  
The document to describe, as a `JSON` value or `VARCHAR` JSON text.

### Return type
<a name="athena-duckdb-functions-json-json-structure-return-type"></a>

`JSON`

### Examples
<a name="athena-duckdb-functions-json-json-structure-examples"></a>

The following SQL query returns the structure of an object that has an array of integers and a string.

```sql
SELECT
    json_structure('{"a": [1, 2], "b": "x"}') AS result
```

The query returns the following result.


| result | 
| --- | 
| `{"a":["UBIGINT"],"b":"VARCHAR"}` | 

The following SQL query describes an array of objects that do not all have the same members. The members are merged into one object.

```sql
SELECT
    json_structure(
        '[{"id": 1, "name": "alice"}, {"id": 2, "email": "b"}]'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[{"id":"UBIGINT","name":"VARCHAR","email":"VARCHAR"}]` | 

The following SQL query shows how element types combine. A number and a string do not combine, so the element type is `"JSON"`. An integer and a number with a fraction combine to `"DOUBLE"`.

```sql
SELECT
    json_structure('[1, "x"]') AS mixed,
    json_structure('[1, 2.5]') AS numbers
```

The query returns the following result.


| mixed | numbers | 
| --- | --- | 
| `["JSON"]` | `["DOUBLE"]` | 

The following SQL query passes the structure of a document to `json_transform`, which converts the document to a `STRUCT`.

```sql
SELECT
    json_transform(
        '{"a": [1, 2], "b": "x"}',
        json_structure('{"a": [1, 2], "b": "x"}')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': [1, 2], 'b': x}` | 

### Considerations
<a name="athena-duckdb-functions-json-json-structure-considerations"></a>

A non-negative integer is described as `"UBIGINT"`, and a negative integer as `"BIGINT"`. In an array, non-negative and negative integers combine to `"HUGEINT"`. An empty object is described as `"JSON"`, and an empty array as `["NULL"]`.

Text that is not valid JSON makes the query fail.

## `json_transform` function
<a name="athena-duckdb-functions-json-json-transform"></a>

Converts `json` to a SQL value whose type is described by `structure`. In `structure`, a JSON object stands for a `STRUCT` with one field for each member. An array with one element stands for a list of that element's type, and a string such as `"INTEGER"` or `"DATE"` names a SQL type. Members of `json` that `structure` does not name are ignored.

The conversion is lenient. A member that `structure` names but `json` does not have becomes `NULL`, and so does a value that cannot be converted to its type. If `json` is `NULL`, the result is `NULL`. To make the query fail in those cases instead, use the [`json_transform_strict` function](#athena-duckdb-functions-json-json-transform-strict). `from_json` takes the same arguments and returns the same result.

### Syntax
<a name="athena-duckdb-functions-json-json-transform-syntax"></a>

```sql
json_transform({{json}}, {{structure}})
```

### Arguments
<a name="athena-duckdb-functions-json-json-transform-arguments"></a>

The `json_transform` function takes the following arguments:

**{{json}}**  
The document to convert, as a `JSON` value or `VARCHAR` JSON text.

**{{structure}}**  
The result type, described by JSON text in a constant `VARCHAR` value, for example `'{"id": "INTEGER", "tags": ["VARCHAR"]}'`. You can write a list type as an array, as in `["INTEGER"]`, or as a type name, as in `"INTEGER[]"`.

### Return type
<a name="athena-duckdb-functions-json-json-transform-return-type"></a>

The type that `structure` describes. For example, the result type for `'{"b": "VARCHAR"}'` is `STRUCT(b VARCHAR)`.

### Examples
<a name="athena-duckdb-functions-json-json-transform-examples"></a>

The following SQL query converts one member of a JSON object to a `STRUCT` field. The member `a` is not in the structure, so it is ignored.

```sql
SELECT
    json_transform(
        '{"a": [1, 2], "b": "x"}', '{"b": "VARCHAR"}'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'b': x}` | 

The following SQL query converts an array to a list of integers, and asks for a member that the document does not have. The missing member becomes `NULL`.

```sql
SELECT
    json_transform(
        '{"a": [1, 2], "b": "x"}',
        '{"a": "INTEGER[]", "c": "DOUBLE"}'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': [1, 2], 'c': NULL}` | 

The following SQL query converts a string that is not a number to `INTEGER`. The value does not convert, so the field is `NULL`, and the query does not fail.

```sql
SELECT
    json_transform(
        '{"a": "abc", "b": 7}',
        '{"a": "INTEGER", "b": "INTEGER"}'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': NULL, 'b': 7}` | 

The following SQL query converts a JSON column and reads one field of the result. The second document has no `name` member, so its `customer_name` is `NULL`.

```sql
SELECT
    n AS doc_id,
    json_transform(
        j, '{"id": "INTEGER", "name": "VARCHAR"}'
    ).name AS customer_name FROM (VALUES
        (1, '{"id": 1, "name": "alice"}'),
        (2, '{"id": 2}')
    ) AS v(n, j) ORDER BY doc_id
```

The query returns the following results.


| doc\_id | customer\_name | 
| --- | --- | 
| `1` | `alice` | 
| `2` | `NULL` | 

### Considerations
<a name="athena-duckdb-functions-json-json-transform-considerations"></a>

`structure` must be a constant. A column, or another expression that is not constant, makes the query fail.

Member names are case sensitive. A `structure` member named `a` does not match a JSON member named `A`, so that field is `NULL`.

A number with a fraction that is converted to an integer type is rounded to the nearest integer. A value exactly halfway is rounded to the even integer. For example, `1.7` becomes `2`, and `2.5` also becomes `2`. A number that is out of range for its type, such as `300` for `TINYINT`, becomes `NULL`.

Text that is not valid JSON makes the query fail.

## `json_transform_strict` function
<a name="athena-duckdb-functions-json-json-transform-strict"></a>

Converts `json` to a SQL value whose type is described by `structure`, in the same way as `json_transform`, but without the lenient handling. The query fails when `json` does not have a member that `structure` names. It also fails when a value cannot be converted to its type, for example the string `"abc"` to `INTEGER`, or the number `300` to `TINYINT`.

Members of `json` that `structure` does not name are still ignored, and a JSON `null` value becomes `NULL`. If `json` is `NULL`, the result is `NULL`. `from_json_strict` takes the same arguments and returns the same result.

### Syntax
<a name="athena-duckdb-functions-json-json-transform-strict-syntax"></a>

```sql
json_transform_strict({{json}}, {{structure}})
```

### Arguments
<a name="athena-duckdb-functions-json-json-transform-strict-arguments"></a>

The `json_transform_strict` function takes the following arguments:

**{{json}}**  
The document to convert, as a `JSON` value or `VARCHAR` JSON text.

**{{structure}}**  
The result type, described by JSON text in a constant `VARCHAR` value, for example `'{"id": "INTEGER", "tags": ["VARCHAR"]}'`. For more information about the format, see [`json_transform` function](#athena-duckdb-functions-json-json-transform).

### Return type
<a name="athena-duckdb-functions-json-json-transform-strict-return-type"></a>

The type that `structure` describes. For example, the result type for `'{"b": "VARCHAR"}'` is `STRUCT(b VARCHAR)`.

### Examples
<a name="athena-duckdb-functions-json-json-transform-strict-examples"></a>

The following SQL query converts a JSON object to a `STRUCT`.

```sql
SELECT
    json_transform_strict(
        '{"b": "x"}', '{"b": "VARCHAR"}'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'b': x}` | 

The following SQL query shows that a member that is not in the structure, `z`, is ignored and does not make the query fail.

```sql
SELECT
    json_transform_strict(
        '{"b": "x", "z": 1}', '{"b": "VARCHAR"}'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'b': x}` | 

The following SQL query converts a number to `VARCHAR` and rounds a number with a fraction to `INTEGER`. Both conversions succeed.

```sql
SELECT
    json_transform_strict(
        '{"a": 1, "b": 1.7}',
        '{"a": "VARCHAR", "b": "INTEGER"}'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': 1, 'b': 2}` | 

### Considerations
<a name="athena-duckdb-functions-json-json-transform-strict-considerations"></a>

`structure` must be a constant. A column, or another expression that is not constant, makes the query fail.

Values are still converted between compatible types. A JSON number converts to a `VARCHAR` field. A number with a fraction that is converted to an integer type is rounded to the nearest integer, and a value exactly halfway is rounded to the even integer. For example, `1.7` becomes `2`, and `2.5` also becomes `2`.