

# JSON inspection functions

Use these functions to find the type of a JSON value, the keys of an object, or the number of elements in an array.

You can also test whether text is valid JSON, whether a document has a value at a path, or whether it contains another JSON value.

## `json_array_length` function
<a name="athena-duckdb-functions-json-json-array-length"></a>

Returns the number of elements in the JSON array `json`, or in the array that `path` selects. Only the top level is counted, so a nested array counts as one element.

If the value is not an array, for example an object, a string, or the JSON value `null`, the result is `0`. If `json` is `NULL` or `path` selects nothing, the result is `NULL`. With a list of paths, the function returns a list that has one length for each path.

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

```sql
json_array_length({{json}} [, {{path}} ])
json_array_length({{json}}, {{path_list}})
```

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

The `json_array_length` function takes the following arguments:

**{{json}}**  
The document that holds the array, as a `JSON` value or as JSON text in a `VARCHAR` value.

**{{path}}**  
The location of the array to count, as a `VARCHAR` JSON path. The default is `'$'`, the whole document. This argument is optional.

**{{path\_list}}**  
The locations of the arrays to count, as a `VARCHAR[]` list of JSON paths.

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

`UBIGINT`. With a list of paths, or with a path that has a wildcard, the result is `UBIGINT[]`.

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

The following SQL query counts the elements of an array.

```sql
SELECT json_array_length('[1, 2, 3]') AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query counts the elements of the array that the path `'$.a'` selects. The inner array `[3, 4]` counts as one element.

```sql
SELECT
    json_array_length(
        '{"a": [1, 2, [3, 4]]}', '$.a'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query shows that an object, which is not an array, has a length of `0`, and that a path that selects nothing returns `NULL`.

```sql
SELECT
    json_array_length('{"x": 1}') AS object_length,
    json_array_length(
        '{"a": [1, 2]}', '$.b'
    ) AS missing_path
```

The query returns the following result.


| object\_length | missing\_path | 
| --- | --- | 
| `0` | `NULL` | 

The following SQL query returns `NULL` instead of `0` for a value that is not an array.

```sql
SELECT
    CASE WHEN json_type(doc) = 'ARRAY' THEN json_array_length(doc) END AS result
    FROM (VALUES
        ('{"x": 1}')
    ) AS v(doc)
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-json-json-array-length-differences-from-engine-version-3"></a>

In Athena engine version 3, `json_array_length` returns `NULL` when the value is not a JSON array. In DuckDB, it returns `0`. To get `NULL`, test the type first, as in `CASE WHEN json_type({{json}}) = 'ARRAY' THEN json_array_length({{json}}) END`.

## `json_contains` function
<a name="athena-duckdb-functions-json-json-contains"></a>

Returns `true` if `needle` appears in `haystack` at any depth. A scalar, such as `'2'` or `'"x"'`, matches an equal value. An object matches when `haystack`, or an object nested in it, has every member of `needle` with a matching value. An array matches when each of its elements is in an array of `haystack`, in any order.

Values must have the same JSON type to match, so the number `1` does not match the string `"1"`, and `1` does not match `1.0`. A key name alone does not match: `json_contains('{"a": 1}', '"a"')` returns `false`. If either argument is `NULL`, the result is `NULL`.

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

```sql
json_contains({{haystack}}, {{needle}})
```

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

The `json_contains` function takes the following arguments:

**{{haystack}}**  
The document to search, as a `JSON` value or as JSON text in a `VARCHAR` value.

**{{needle}}**  
The value to find, as a `JSON` value or as JSON text in a `VARCHAR` value. To find a string, include its double quotes, as in `'"x"'`.

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

`BOOLEAN`

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

The following SQL query searches a document for the JSON string `"x"`.

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

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query searches for an object member whose array holds `2`, and for two elements of an array in a different order.

```sql
SELECT
    json_contains(
        '{"a": [1, 2], "b": "x"}', '{"a": [2]}'
    ) AS has_member,
    json_contains('[1, 2, 3]', '[3, 1]') AS has_elements
```

The query returns the following result.


| has\_member | has\_elements | 
| --- | --- | 
| `true` | `true` | 

The following SQL query shows two searches that do not match. String comparison is case-sensitive, and a number does not match a string that holds the same digits.

```sql
SELECT
    json_contains(
        '{"tags": ["red", "blue"]}', '"BLUE"'
    ) AS different_case,
    json_contains('["1"]', '1') AS number_and_string
```

The query returns the following result.


| different\_case | number\_and\_string | 
| --- | --- | 
| `false` | `false` | 

The following SQL query converts a SQL string to JSON with `to_json` before it searches for it.

```sql
SELECT
    json_contains(
        '{"tags": ["red", "blue"]}', to_json('blue')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

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

`needle` must be valid JSON text. A bare word such as `'x'` makes the query fail, and a number literal such as `2` that is not in quotes also makes the query fail. A typed number, such as an `INTEGER` column or `CAST(2 AS INTEGER)`, is converted to JSON, so `json_contains('[1, 2]', CAST(2 AS INTEGER))` returns `true`. To search for a SQL value, convert it with `to_json`, as in `json_contains({{haystack}}, to_json({{value}}))`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-json-json-contains-differences-from-engine-version-3"></a>

Athena engine version 3 has `json_array_contains({{json}}, {{value}})`, which tests whether a JSON array has an element equal to a SQL value. DuckDB does not have `json_array_contains`. Use `json_contains` and pass the value as JSON text, as in `json_contains('[1, 2, 3]', '2')`. The `json_contains` function also searches nested arrays and objects, so it can return `true` where `json_array_contains` returns `false`.

## `json_exists` function
<a name="athena-duckdb-functions-json-json-exists"></a>

Returns `true` if `path` selects a value in `json`, and `false` if it does not. A member whose value is the JSON value `null` exists, so the result is `true` for it. If `json` or `path` is `NULL`, the result is `NULL`. With a list of paths, the function returns a list that has one result for each path.

Use `json_exists` to tell a missing member from a member whose value is `null`. The `->>` operator returns `NULL` for both.

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

```sql
json_exists({{json}}, {{path}})
json_exists({{json}}, {{path_list}})
```

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

The `json_exists` function takes the following arguments:

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

**{{path}}**  
The location to look for, as a `VARCHAR` JSON path.

**{{path\_list}}**  
The locations to look for, as a `VARCHAR[]` list of JSON paths.

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

`BOOLEAN`. With a list of paths, or with a path that has a wildcard, the result is `BOOLEAN[]`.

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

The following SQL query tests whether a document has the member `a`.

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

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query compares a member whose value is `null` with a member that does not exist.

```sql
SELECT
    json_exists('{"a": null}', '$.a') AS null_member,
    json_exists('{"a": null}', '$.b') AS missing_member
```

The query returns the following result.


| null\_member | missing\_member | 
| --- | --- | 
| `true` | `false` | 

The following SQL query tests two array positions with a list of paths. The array has an element at position `1` but none at position `5`.

```sql
SELECT
    json_exists(
        '{"a": [1, 2]}', ['$.a[1]', '$.a[5]']
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[true, false]` | 

The following SQL query shows that a path with the `lax` mode returns `false`, and that the same path without the mode returns `true`.

```sql
SELECT
    json_exists('{"a": 1}', 'lax $.a') AS with_mode,
    json_exists('{"a": 1}', '$.a') AS without_mode
```

The query returns the following result.


| with\_mode | without\_mode | 
| --- | --- | 
| `false` | `true` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-json-json-exists-differences-from-engine-version-3"></a>

In Athena engine version 3, `json_exists` takes a SQL/JSON path that starts with a mode, such as `'lax $.a'`. DuckDB does not support path modes. A path that starts with `lax` or `strict` does not cause an error, and `json_exists` returns `false`. Remove the mode, as in `json_exists({{json}}, '$.a')`.

## `json_keys` function
<a name="athena-duckdb-functions-json-json-keys"></a>

Lists the key names of the top-level object in `json`, or of the object at `path` when you pass one. The result is a list of `VARCHAR` values. The keys are in the order in which they appear in the document, and a key that appears twice is listed twice. To count the keys of an object, use `len(json_keys({{json}}))`.

For a value that is not an object, such as an array or a number, the function returns an empty list. If `path` does not exist, or an argument is `NULL`, the result is `NULL`. When you pass a list of paths, the function returns one list of keys for each path. When `path` contains a wildcard, such as `'$.*'`, the function returns one list of keys for each value that matches.

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

```sql
json_keys({{json}} [, {{path}} ])
json_keys({{json}}, {{path_list}})
```

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

The `json_keys` function takes the following arguments:

**{{json}}**  
The document to read the keys from, as a `JSON` value or a `VARCHAR` value that holds JSON text.

**{{path}}**  
The location of the object, as a `VARCHAR` in JSONPath or JSON Pointer syntax. Without `path`, the function reads the top-level object. This argument is optional.

**{{path\_list}}**  
The locations of several objects, as a `VARCHAR[]` list such as `['$.a', '$.b']`, to read the keys of each one in one call.

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

`VARCHAR[]`, or `VARCHAR[][]` when you pass a list of paths or a path that contains a wildcard.

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

The following SQL query returns the keys of a JSON object.

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

The query returns the following result.


| result | 
| --- | 
| `[a, b]` | 

The following SQL query returns the keys of the nested object at `$.a`.

```sql
SELECT
    json_keys(
        '{"a": {"x": 1, "y": 2}, "b": 3}', '$.a'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[x, y]` | 

The following SQL query passes two paths and returns one list of keys for each.

```sql
SELECT
    json_keys(
        '{"a": {"x": 1}, "b": {"y": 2, "z": 3}}',
        ['$.a', '$.b']
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[[x], [y, z]]` | 

The following SQL query shows that an array has no keys, so the list is empty, and that a path that does not exist returns `NULL`.

```sql
SELECT
    len(json_keys('[1, 2]')) AS array_key_count,
    json_keys(
        '{"a": 1}', '$.missing'
    ) IS NULL AS missing_is_null
```

The query returns the following result.


| array\_key\_count | missing\_is\_null | 
| --- | --- | 
| `0` | `true` | 

## `json_type` function
<a name="athena-duckdb-functions-json-json-type"></a>

Returns the type of `json` as a `VARCHAR` value: `OBJECT`, `ARRAY`, `VARCHAR` for a string, `BOOLEAN`, `NULL` for JSON `null`, or a numeric type. A non-negative integer is `UBIGINT`, a negative integer is `BIGINT`, and a number with a fraction or an exponent is `DOUBLE`.

With `path`, returns the type of the value at that path. If the path does not match anything, the result is SQL `NULL`, which is different from the string `'NULL'` that the function returns for JSON `null`. With a list of paths, returns a list with one type for each path. If `json` is `NULL`, the result is `NULL`.

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

```sql
json_type({{json}} [, {{path}} ])
```

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

The `json_type` function takes the following arguments:

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

**{{path}}**  
The location of the value to inspect, as a `VARCHAR` JSON path, such as `'$.a'` or `'$.a[0]'`, or a list of paths. This argument is optional.

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

`VARCHAR`, or `VARCHAR[]` when `path` is a list.

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

The following SQL query returns the type of a JSON document.

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

The query returns the following result.


| result | 
| --- | 
| `OBJECT` | 

The following SQL query returns the types of the values at two paths, and of a path that does not match anything. For the path that does not match, the result is SQL `NULL`.

```sql
SELECT
    json_type(
        '{"a": [1, 2], "b": "x"}', '$.a'
    ) AS a_type,
    json_type(
        '{"a": [1, 2], "b": "x"}', '$.a[0]'
    ) AS element_type,
    json_type(
        '{"a": [1, 2], "b": "x"}', '$.c'
    ) AS missing_type
```

The query returns the following result.


| a\_type | element\_type | missing\_type | 
| --- | --- | --- | 
| `ARRAY` | `UBIGINT` | `NULL` | 

The following SQL query tells a JSON `null` value apart from a missing member. The first returns the string `'NULL'`, and the second returns SQL `NULL`.

```sql
SELECT
    json_type(
        '{"a": null}', '$.a'
    ) = 'NULL' AS is_json_null,
    json_type(
        '{"a": null}', '$.b'
    ) IS NULL AS is_missing
```

The query returns the following result.


| is\_json\_null | is\_missing | 
| --- | --- | 
| `true` | `true` | 

The following SQL query passes a list of paths and returns a list of types.

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

The query returns the following result.


| result | 
| --- | 
| `[ARRAY, VARCHAR]` | 

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

Text that is not valid JSON makes the query fail. To test text before you call `json_type`, use the [`json_valid` function](#athena-duckdb-functions-json-json-valid).

## `json_valid` function
<a name="athena-duckdb-functions-json-json-valid"></a>

Returns `true` if `json` parses as JSON text, and `false` if it does not. Any JSON value counts, including a scalar such as `42` or `"x"`, and spaces around the value are allowed. If `json` is `NULL`, the result is `NULL`.

Use `json_valid` to find or filter out text that would make other JSON functions fail.

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

```sql
json_valid({{json}})
```

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

The `json_valid` function takes the following argument:

**{{json}}**  
The text to test, as a `VARCHAR` value. A `JSON` value is also accepted.

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

`BOOLEAN`

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

The following SQL query tests a complete JSON object and one that is missing its closing brace.

```sql
SELECT
    json_valid('{"a": 1}') AS valid_object,
    json_valid('{"a": 1') AS missing_brace
```

The query returns the following result.


| valid\_object | missing\_brace | 
| --- | --- | 
| `true` | `false` | 

The following SQL query tests the values of a column. A scalar such as `42` is valid JSON, and a `NULL` input returns `NULL`.

```sql
SELECT
    v.id AS row_id,
    json_valid(j) AS is_valid FROM (VALUES
        (1, '{"a": 1}'),
        (2, '[1, 2'),
        (3, '42'),
        (4, NULL)
    ) AS v(id, j) ORDER BY row_id
```

The query returns the following results.


| row\_id | is\_valid | 
| --- | --- | 
| `1` | `true` | 
| `2` | `false` | 
| `3` | `true` | 
| `4` | `NULL` | 

The following SQL query shows that a comma after the last member is accepted, but a member name without double quotes is not.

```sql
SELECT
    json_valid('{"a": 1,}') AS trailing_comma,
    json_valid('{a: 1}') AS unquoted_name
```

The query returns the following result.


| trailing\_comma | unquoted\_name | 
| --- | --- | 
| `true` | `false` | 

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

The parser accepts some text that strict JSON parsers reject: a comma after the last member or element, as in `'{"a": 1,}'`, and the values `NaN` and `Infinity`. It rejects member names without double quotes, comments, and text that has more than one value.

An empty string is not valid JSON, so `json_valid('')` returns `false`.