View a markdown version of this page

JSON inspection functions - Amazon Athena

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

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

json_array_length(json [, path ]) json_array_length(json, path_list)

Arguments

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

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

Examples

The following SQL query counts the elements of an array.

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.

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.

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.

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

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

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

json_contains(haystack, needle)

Arguments

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

BOOLEAN

Examples

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

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.

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.

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.

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

The query returns the following result.

result

true

Considerations

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

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

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

json_exists(json, path) json_exists(json, path_list)

Arguments

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

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

Examples

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

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.

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.

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.

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

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

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

json_keys(json [, path ]) json_keys(json, path_list)

Arguments

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

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

Examples

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

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.

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.

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.

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

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

json_type(json [, path ])

Arguments

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

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

Examples

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

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.

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.

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.

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

The query returns the following result.

result

[ARRAY, VARCHAR]

Considerations

Text that is not valid JSON makes the query fail. To test text before you call json_type, use the json_valid function.

json_valid function

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

json_valid(json)

Arguments

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

BOOLEAN

Examples

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

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.

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.

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

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.