View a markdown version of this page

JSON extraction functions and operators - Amazon Athena

JSON extraction functions and operators

Use these functions and the -> and ->> operators to read the value at a JSON path, either as JSON or as text.

You can also return the elements of an array, the members of an object, or every nested value as rows.

->> (extract as text) operator

Returns the part of json that path selects, as a VARCHAR value. A JSON string is returned without its double quotes, and a number, Boolean value, object, or array is returned as its JSON text. If json is NULL, if the path selects nothing, or if the selected value is the JSON value null, the result is NULL.

With an integer instead of a path, the operator returns the array element at that position, where the first element is 0 and a negative position counts from the end. With a list of paths, it returns a list that has one value for each path. json ->> path returns the same result as json_extract_string(json, path). To get the value as JSON instead of text, use the -> operator or json_extract.

Syntax

json ->> path json ->> index json ->> path_list

Arguments

The ->> (extract as text) operator takes the following arguments:

json

The document to read from, as a JSON value or as JSON text in a VARCHAR value.

path

The location of the value, as a VARCHAR JSON path, such as '$.a.b', or a JSON pointer, such as '/a/b'.

index

The position of the element in a JSON array, as a BIGINT. The first element is 0, and -1 is the last element.

path_list

The locations of the values, as a VARCHAR[] list of JSON paths.

Return type

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

Examples

The following SQL query reads a nested number as text.

SELECT '{"a": {"b": 42}}'::JSON ->> '$.a.b' AS result

The query returns the following result.

result

42

The following SQL query reads a nested string and the first element of an array from each row. The second document has no items array, so its first_item is NULL.

SELECT doc ->> '$.customer.name' AS customer, doc ->> '$.items[0]' AS first_item FROM (VALUES ( 1, '{"customer": {"name": "alice"}, "items": ["pen", "ink"]}' ), (2, '{"customer": {"name": "bob"}}') ) AS v(id, doc) ORDER BY id

The query returns the following results.

customer first_item

alice

pen

bob

NULL

The following SQL query passes an array position instead of a path. Position 0 is the first element, and -1 is the last.

SELECT '[10, 20, 30]' ->> 0 AS first_element, '[10, 20, 30]' ->> -1 AS last_element

The query returns the following result.

first_element last_element

10

30

The following SQL query casts the text result to DECIMAL before it multiplies the value. The second column shows that a number is returned in the form that the JSON parser produces, so 1.50 becomes 1.5.

SELECT CAST( '{"price": 19.99}' ->> '$.price' AS DECIMAL(10, 2) ) * 2 AS doubled_price, '{"n": 1.50}' ->> '$.n' AS number_text

The query returns the following result.

doubled_price number_text

39.98

1.5

Considerations

Arithmetic operators such as + bind more tightly than ->>, so json ->> '$.a' + 1 tries to add 1 to the path, and the query fails. The result is text in any case, so cast it before you use it in arithmetic, as in CAST(json ->> '$.a' AS INTEGER) + 1.

A number is returned in the form that the JSON parser produces, not as it is written in the document. For example, 1.50 is returned as 1.5, and 1e2 is returned as 100.0.

Differences from Athena engine version 3

Athena engine version 3 uses json_extract_scalar(json, path) to get a JSON value as text. DuckDB has no json_extract_scalar function. Use json ->> path or json_extract_string(json, path) instead.

In Athena engine version 3, json_extract_scalar returns NULL when the path selects an object or an array. In DuckDB, ->> returns the object or array as JSON text, such as [1,2]. To get NULL for objects and arrays, test the type first, as in CASE WHEN json_type(json, path) NOT IN ('OBJECT', 'ARRAY') THEN json ->> path END.

Athena engine version 3 returns a number as it is written in the document, so json_extract_scalar('{"n": 1.50}', '$.n') returns 1.50. In DuckDB, '{"n": 1.50}' ->> '$.n' returns 1.5.

JSON paths

Many JSON functions and operators take a path that selects part of a JSON value. A path usually starts with $, which stands for the whole value. Add .key to step into a member of an object, and [n] to step into an element of an array, where the first element is 0. For example, '$.customer.name' selects the name member of the customer object, and '$.items[0]' selects the first element of the items array.

To count from the end of an array, use a negative position or #-n, so '$.items[-1]' and '$.items[#-1]' both select the last element. To select every element of an array or every member of an object, use [*] or .*.

To find a key at any depth, use .., as in '$..sku'. A path that has * or .. can match more than one value, so the functions return a list for it.

If a key contains a period or another special character, put the key in double quotes, as in '$."order.id"'. Key names are case-sensitive. A path that does not start with $ or / is read as one key name, so 'order.id' selects a member named order.id. A path that starts with / is a JSON pointer, in which / separates the keys and array positions, as in '/items/0'.

When a path selects nothing, for example a key that does not exist or a position past the end of an array, most functions return NULL. The json_exists function returns false, and the json_each function returns no rows.

When a path that has * or .. selects nothing, the functions return an empty list instead of NULL, so IS NULL is false for the result. A path that has a syntax error, such as a period at the end, makes the query fail.

Examples

The following SQL query reads a nested member, the first element of an array, and the last element of the same array.

SELECT doc ->> '$.customer.name' AS customer_name, doc ->> '$.items[0]' AS first_item, doc ->> '$.items[-1]' AS last_item FROM (VALUES ( '{"customer": {"name": "alice"}, "items": ["pen", "ink", "pad"]}' ) ) AS v(doc)

The query returns the following result.

customer_name first_item last_item

alice

pen

pad

The following SQL query uses a wildcard and a search at any depth. Both paths can match more than one value, so each result is a list.

SELECT doc ->> '$.items[*].sku' AS item_skus, doc ->> '$..sku' AS all_skus FROM (VALUES ('{"items": [{"sku": "a1"}, {"sku": "b2"}]}') ) AS v(doc)

The query returns the following result.

item_skus all_skus

[a1, b2]

[a1, b2]

The following SQL query reads a key that contains a period, once with a quoted key and once as a plain key name. It also reads an array element with a JSON pointer.

SELECT doc ->> '$."order.id"' AS quoted_key, doc ->> 'order.id' AS plain_key, doc ->> '/items/1' AS json_pointer FROM (VALUES ('{"order.id": 7, "items": [10, 20]}') ) AS v(doc)

The query returns the following result.

quoted_key plain_key json_pointer

7

7

20

json_each function

Returns a table with one row for each element when json is a JSON array, or one row for each member when json is a JSON object, in document order. With path, the function expands the value that the path selects instead. Only the top level is expanded. To expand every level, use the json_tree function.

A scalar value returns one row. NULL, an empty array or object, and a path that selects nothing return no rows.

Call json_each in the FROM clause. To expand a JSON column, list json_each after the table that has the column, and pass the column, as in FROM orders AS o, json_each(o.doc).

Syntax

json_each(json [, path ])

Arguments

The json_each function takes the following arguments:

json

The document to expand, as a JSON value or as JSON text in a VARCHAR value.

path

The location of the array or object to expand, as a VARCHAR JSON path. The path cannot have * or .., or the query fails. This argument is optional.

Return type

A table with the following columns. The key column (VARCHAR) holds the member name, or the array position as text. The value column (JSON) holds the element or member value. The type column (VARCHAR) holds the type of value, which is OBJECT, ARRAY, VARCHAR, UBIGINT, BIGINT, DOUBLE, BOOLEAN, or NULL. The atom column (JSON) holds the value for a scalar, or NULL for an object or array. The id column (UBIGINT) holds a number that increases in document order. The parent column (UBIGINT) is always NULL for json_each. The fullkey column (VARCHAR) holds the path of the element, such as $.a. The path column (VARCHAR) holds the path of the value that was expanded, such as $.

Examples

The following SQL query returns one row for each member of an object.

SELECT key AS member, value AS member_value, type AS json_type FROM json_each( '{"a": [1, 2], "b": "x"}' ) ORDER BY member

The query returns the following results.

member member_value json_type

a

[1,2]

ARRAY

b

"x"

VARCHAR

The following SQL query expands the array that a path selects and returns the full path of each element.

SELECT fullkey AS element_path, value AS element FROM json_each( '{"a": {"b": [10, 20]}}', '$.a.b' ) ORDER BY element_path

The query returns the following results.

element_path element

$.a.b[0]

10

$.a.b[1]

20

The following SQL query expands the tags array of each row. The ->> operator returns each tag as text.

SELECT v.order_id AS order_id, e.value ->> '$' AS tag FROM (VALUES (1, '{"tags": ["red", "blue"]}'), (2, '{"tags": ["green"]}') ) AS v(order_id, doc), json_each(v.doc, '$.tags') AS e ORDER BY order_id, e.id

The query returns the following results.

order_id tag

1

red

1

blue

2

green

The following SQL query counts the rows that json_each returns for a path that selects nothing. The count is 0.

SELECT count(*) AS row_count FROM json_each( '{"a": 1}', '$.missing' )

The query returns the following result.

row_count

0

json_extract function and -> operator

Returns the part of json that path selects, as a JSON value. A JSON string keeps its double quotes, so json_extract('{"b": "x"}', '$.b') returns "x". With an integer instead of a path, the function returns the array element at that position, where the first element is 0. With a list of paths, it returns a list that has one value for each path.

If json or path is NULL, or if the path selects nothing, the result is NULL. If the selected value is the JSON value null, the result is that JSON value, not SQL NULL.

The -> operator is another way to call json_extract: json -> path returns the same result as json_extract(json, path). Because the result is JSON, you can apply -> again to step further into it. To get a value as text without the quotes, use the ->> operator. json_extract_path takes the same arguments as json_extract and returns the same result.

Syntax

json_extract(json, path) json_extract(json, index) json_extract(json, path_list) json -> path json -> index json -> path_list

Arguments

The json_extract function and -> operator takes the following arguments:

json

The document to read from, as a JSON value or as JSON text in a VARCHAR value.

path

The location of the value, as a VARCHAR JSON path, such as '$.a.b', or a JSON pointer, such as '/a/b'.

index

The position of the element in a JSON array, as a BIGINT. The first element is 0, and -1 is the last element.

path_list

The locations of the values, as a VARCHAR[] list of JSON paths.

Return type

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

Examples

The following SQL query extracts a string member. The result is a JSON string, so it keeps its double quotes.

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

The query returns the following result.

result

"x"

The following SQL query uses the -> operator to extract an object and a member nested in it.

SELECT '{"a": {"b": "x"}}'::JSON -> '$.a' AS outer_value, '{"a": {"b": "x"}}'::JSON -> '$.a.b' AS inner_value

The query returns the following result.

outer_value inner_value

{"b":"x"}

"x"

The following SQL query extracts two members with a list of paths.

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

The query returns the following result.

result

[1, "x"]

The following SQL query shows that a member whose value is null returns the JSON value null, which is not SQL NULL, and that a missing member returns SQL NULL.

SELECT json_extract( '{"a": null}', '$.a' ) IS NULL AS null_member_is_sql_null, json_extract( '{"a": 1}', '$.b' ) IS NULL AS missing_member_is_sql_null

The query returns the following result.

null_member_is_sql_null missing_member_is_sql_null

false

true

Considerations

To use the result as a SQL number, cast it, as in CAST(json_extract(json, '$.a') AS INTEGER). A cast to VARCHAR keeps the double quotes of a JSON string, so to get a string as text, use the ->> operator or the json_extract_string function.

Arithmetic operators such as + bind more tightly than ->, so json -> '$.a' + 1 tries to add 1 to the path, and the query fails. Cast the extraction instead, as in CAST(json -> '$.a' AS INTEGER) + 1.

Differences from Athena engine version 3

In Athena engine version 3, a path can name a key in brackets, as in '$["a b"]'. DuckDB does not accept brackets around a key, and the query fails. Write the key after a period, in double quotes, as in '$."a b"'.

json_extract_string function

Returns the value at path in json as a VARCHAR. For a JSON string, the function removes the quotes and decodes the escape sequences. For a number or boolean, it returns the JSON text, and for an array or object, it returns compact JSON text. To get the value as JSON instead, use json_extract.

The result is NULL when path does not exist, when the value at path is the JSON null, or when an argument is NULL. When you pass a list of paths, the function returns a list with one element for each path, in the same order.

The ->> operator returns the same result, so json ->> path equals json_extract_string(json, path).

json_extract_path_text is an alias of json_extract_string.

Syntax

json_extract_string(json, path) json_extract_string(json, index) json_extract_string(json, path_list)

Arguments

The json_extract_string function takes the following arguments:

json

The document to read from, as a JSON value or a VARCHAR value that holds JSON text.

path

The location of the value, as a VARCHAR. Use JSONPath syntax that starts with $, such as '$.a.b' or '$.items[0]', or JSON Pointer syntax that starts with /, such as '/a/b'. In an array, [#-1] selects the last element. Any other string is read as a single key name.

index

The position of the element in a JSON array, as a BIGINT counted from 0. A negative index counts from the end, so -1 selects the last element.

path_list

The locations of several values to read in one call, as a VARCHAR[] list of paths, such as ['$.a', '$.b'].

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 string at the path $.b, without its quotes.

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

The query returns the following result.

result

x

The following SQL query reads the first and the last element of a JSON array by position. Positions start at 0, and a negative position counts from the end.

SELECT json_extract_string( '[10, 20, 30]', 0 ) AS first_element, json_extract_string( '[10, 20, 30]', -1 ) AS last_element

The query returns the following result.

first_element last_element

10

30

The following SQL query passes a list of two paths, so the result is a list with one value for each path.

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

The query returns the following result.

result

[1, x]

The following SQL query shows three results that can be unexpected. An array is returned as JSON text, the number 1.50 is returned as 1.5, and a key that does not exist returns NULL.

SELECT json_extract_string( '{"a": [1, 2], "n": 1.50}', '$.a' ) AS array_text, json_extract_string( '{"a": [1, 2], "n": 1.50}', '$.n' ) AS number_text, json_extract_string( '{"a": [1, 2], "n": 1.50}', '$.missing' ) AS missing_key

The query returns the following result.

array_text number_text missing_key

[1,2]

1.5

NULL

Considerations

If json is not valid JSON, the query fails. To skip rows that do not hold valid JSON, test them first, for example CASE WHEN json_valid(json) THEN json_extract_string(json, path) END.

Numbers are returned in a standard format, not as they are written in the document. For example, 1.50 is returned as 1.5.

A path with a wildcard, such as '$.items[*]', returns every matching value as a VARCHAR[].

Differences from Athena engine version 3

Athena engine version 3 names this function json_extract_scalar. In DuckDB, use json_extract_string or the ->> operator.

In Athena engine version 3, json_extract_scalar returns NULL when the path selects an array or an object. In DuckDB, json_extract_string returns the array or object as JSON text. To get the Athena engine version 3 result, check the type first, for example CASE WHEN json_type(json, path) NOT IN ('ARRAY', 'OBJECT') THEN json_extract_string(json, path) END.

In Athena engine version 3, json_extract_scalar returns NULL when the input is not valid JSON. In DuckDB, json_extract_string fails the query. To get NULL instead, test the input with json_valid first.

json_tree function

Walks json and returns one row for the value at the root, and one row for every object member and array element nested in it, at every level. The rows are in document order. With path, the walk starts at the value at that path instead of at the root. If path does not match anything, or json is NULL, the function returns no rows.

Each row has the member name or array index in key, the value as JSON in value, its type in type, and its full path in fullkey. For a string, a number, or a boolean, atom holds the value. For an object, an array, or JSON null, atom is SQL NULL. To return only the top-level members or elements, use the json_each function.

Syntax

json_tree(json [, path ])

Arguments

The json_tree function takes the following arguments:

json

The document to walk, as a JSON value or VARCHAR JSON text.

path

The location of the value to start from, as a VARCHAR JSON path, such as '$.a'. The default is the root, '$'. This argument is optional.

Return type

A table with one row for each value, and the following columns. The key column (VARCHAR) holds the member name or array index, or NULL for the starting value. The value column is JSON. The type column (VARCHAR) holds a type name such as OBJECT, ARRAY, VARCHAR, or UBIGINT. The atom column is JSON, and the id column is UBIGINT. The parent column (UBIGINT) holds the id of the parent row, or NULL for the starting value. The fullkey column (VARCHAR) holds the path of the value. The path column (VARCHAR) holds the path of the parent, or the starting path for the starting value. The hidden columns json and root hold the input document and the starting path. SELECT * does not return them, but you can select them by name.

Examples

The following SQL query returns the path and the type of every value in a document, in document order, and the value as JSON when it is a scalar. An object or an array is not a scalar, so its atom_value is NULL.

SELECT fullkey AS full_key, type AS value_type, atom AS atom_value FROM json_tree( '{"a": [1, 2], "b": "x"}' ) ORDER BY id

The query returns the following results.

full_key value_type atom_value

$

OBJECT

NULL

$.a

ARRAY

NULL

$.a[0]

UBIGINT

1

$.a[1]

UBIGINT

2

$.b

VARCHAR

"x"

The following SQL query starts the walk at the array $.a. The row for the starting value has a NULL key.

SELECT key AS element_key, value AS element_value FROM json_tree( '{"a": [1, 2], "b": "x"}', '$.a' ) ORDER BY id

The query returns the following results.

element_key element_value

NULL

[1,2]

0

1

1

2

The following SQL query walks a JSON column and returns the scalar values of each document.

SELECT v.id AS doc_id, t.fullkey AS full_key, t.atom AS atom_value FROM (VALUES (1, '{"x": 1}'), (2, '{"y": [true]}') ) AS v(id, j), json_tree(v.j) AS t WHERE t.atom IS NOT NULL ORDER BY v.id, t.id

The query returns the following results.

doc_id full_key atom_value

1

$.x

1

2

$.y[0]

true

Considerations

value and atom are JSON values, so a string keeps its double quotes, as in "x". To get the string without the quotes, use the json_extract_string function, as in json_extract_string(atom, '$').

The id values are unique and increase in document order, but they are not consecutive. Use them to order the rows and to match a row with its parent, not to count rows.

json_value function

Returns the value at path in json as VARCHAR JSON text, when that value is a string, a number, or a boolean. If the value is an object, an array, or JSON null, or if the path does not match anything, the result is NULL. If json is NULL, the result is NULL.

The result is JSON text, so a string keeps its double quotes: the string x is returned as "x". To get a string without the quotes, use the json_extract_string function. With index, the function returns the array element at that position, counting from 0. A negative index counts from the end. With a list of paths, the function returns a list with one value for each path.

Syntax

json_value(json, path) json_value(json, index)

Arguments

The json_value function takes the following arguments:

json

The document to read, as a JSON value or VARCHAR JSON text.

path

The location of the value, as a VARCHAR JSON path, such as '$.a.b' or '$.a[0]', or a list of paths. Write the path without a mode keyword such as lax or strict.

index

The position of the element in a JSON array, as a BIGINT. The first element is 0, and -1 is the last element.

Return type

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

Examples

The following SQL query returns an array element by path, and the last element of an array by index.

SELECT json_value( '{"a": [1, 2], "b": "x"}', '$.a[1]' ) AS by_path, json_value('[10, 20, 30]', -1) AS by_index

The query returns the following result.

by_path by_index

2

30

The following SQL query compares json_value with json_extract_string for a string value. json_value keeps the double quotes.

SELECT json_value( '{"a": [1, 2], "b": "x"}', '$.b' ) AS json_text, json_extract_string( '{"a": [1, 2], "b": "x"}', '$.b' ) AS plain_text

The query returns the following result.

json_text plain_text

"x"

x

The following SQL query reads a path whose value is an array. An array is not a scalar, so the result is NULL.

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

The query returns the following result.

result

NULL

The following SQL query uses a path that starts with the mode keyword lax. The path does not match anything, so the result is NULL.

SELECT json_value('{"b": "x"}', 'lax $.b') AS result

The query returns the following result.

result

NULL

Considerations

A path that starts with a SQL/JSON mode keyword, such as 'lax $.b', does not match anything. The query does not fail, and the result is NULL. Remove the keyword from the path.

Differences from Athena engine version 3

In Athena engine version 3, json_value takes a SQL/JSON path that can start with a mode keyword, such as 'lax $.b', and returns a string value without its double quotes. In DuckDB, a path that starts with a mode keyword returns NULL, and a string value keeps its double quotes, so json_value('{"b": "x"}', '$.b') returns "x". Remove the mode keyword from the path, and use the json_extract_string function to get a string without its quotes.