View a markdown version of this page

JSON modification functions - Amazon Athena

JSON modification functions

Use these functions to set, add, replace, or remove the value at a JSON path, or to remove the object members whose value is null.

Other functions merge JSON documents, or return the merge patch that turns one document into another.

json_deep_merge function

Merges each JSON value into the result of the values before it, from left to right. When both values are objects, the members of the later object are added to the earlier one. A member that is an object in both is merged in the same way, at every depth. In all other cases, the later value replaces the earlier one, so an array replaces an earlier array instead of being combined with it.

A member whose value is the JSON value null in a later object is ignored, so it does not remove or change the earlier member. To remove members with null, use the json_merge_patch function.

Syntax

json_deep_merge(json1, json2 [, ...])

Arguments

The json_deep_merge function takes the following arguments:

json1

The first JSON value, as a JSON value or as JSON text in a VARCHAR value.

json2

The value to merge into json1, as a JSON value. You can pass more values, and each one is merged into the result so far.

Return type

JSON

Examples

The following SQL query merges two objects that both have a member a. Because a is an object in both, its members are combined.

SELECT json_deep_merge( '{"a": {"x": 1}}', '{"a": {"y": 2}}' ) AS result

The query returns the following result.

result

{"a":{"x":1,"y":2}}

The following SQL query shows that a later number replaces an earlier one, that a later array replaces an earlier array, and that a later null is ignored. The changed member a moves after b.

SELECT json_deep_merge( '{"a": {"x": 1, "tags": [1, 2]}, "b": 2}', '{"a": {"x": 5, "tags": [3]}, "b": null}' ) AS result

The query returns the following result.

result

{"b":2,"a":{"x":5,"tags":[3]}}

The following SQL query merges three objects. The last value of a takes precedence.

SELECT json_deep_merge( '{"a": 1}', '{"b": 2}', '{"a": 3}' ) AS result

The query returns the following result.

result

{"b":2,"a":3}

The following SQL query shows that a NULL last argument makes the result NULL, and that coalesce with an empty object avoids it.

SELECT json_deep_merge( '{"a": 1}', NULL ) IS NULL AS null_result, json_deep_merge( '{"a": 1}', coalesce(NULL, '{}') ) AS with_coalesce

The query returns the following result.

null_result with_coalesce

true

{"a":1}

Considerations

A NULL argument discards the values before it, so if the last argument is NULL, the result is NULL. To skip NULL arguments instead, replace each one with an empty object, as in coalesce(json, '{}').

The order of members in the result can differ from the inputs. A member that a later object changes can move to the end of its object.

json_insert function

Returns json with value added at path, but only when nothing exists at path. If a value already exists there, the function returns the document unchanged. The function creates any objects that are missing along the path. In an array, a path to the position immediately after the last element, or the position [#], appends the value. A position that already exists, or one further past the end, leaves the array unchanged.

If any argument is NULL, the result is NULL. To add the JSON null, pass the text 'null' as value.

To change only values that already exist, use the json_replace function. To add a value or change it in one call, use the json_set function.

Syntax

json_insert(json, path, value)

Arguments

The json_insert function takes the following arguments:

json

The document to add the value to, as a JSON value or a VARCHAR value that holds JSON text.

path

The location to add the value at, as a VARCHAR in JSONPath syntax, such as '$.a.b' or '$.items[#]', or JSON Pointer syntax, such as '/a/b'.

value

The value to add, as JSON. A VARCHAR value is read as JSON text, so '2' adds a number and '"x"' adds a string. To add a SQL value, convert it with to_json, for example to_json('x').

Return type

JSON

Examples

The following SQL query adds the key b to an object.

SELECT json_insert('{"a": 1}', '$.b', '2') AS result

The query returns the following result.

result

{"a":1,"b":2}

The following SQL query tries to add the key a, which already exists. The document is returned unchanged.

SELECT json_insert('{"a": 1}', '$.a', '2') AS result

The query returns the following result.

result

{"a":1}

The following SQL query adds a string at a nested path. The missing object b is created, and to_json turns the SQL string into a JSON string.

SELECT json_insert( '{"a": 1}', '$.b.c', to_json('x') ) AS result

The query returns the following result.

result

{"a":1,"b":{"c":"x"}}

The following SQL query appends an element to an array with the position [#].

SELECT json_insert( '{"tags": ["red"]}', '$.tags[#]', to_json('blue') ) AS result

The query returns the following result.

result

{"tags":["red","blue"]}

Considerations

A string such as 'x' is not valid JSON text, so passing it as value fails the query. Use to_json('x') or '"x"'. An integer literal such as 2 is not converted for you either, so pass '2' or to_json(2). A BIGINT value such as CAST(2 AS BIGINT), a DECIMAL literal such as 2.5, a BOOLEAN value, and a list are converted to JSON for you.

Paths with wildcards, such as '$.*', are not supported and fail the query.

json_merge_patch function

Applies json2 and each further argument to json1, in order, as a JSON merge patch (RFC 7386), and returns the result. In each patch, a key whose value is the JSON null removes that key, and any other key adds or replaces the key. When the existing value and the patch value are both objects, they are merged key by key. Arrays and all other values replace the existing value as a whole. A patch that is not an object replaces the whole document.

A NULL argument counts as the JSON null and discards the document merged so far. If the last argument is NULL, the result is NULL.

To compute the patch that turns one document into another, use the json_merge_patch_diff function.

Syntax

json_merge_patch(json1, json2 [, ...])

Arguments

The json_merge_patch function takes the following arguments:

json1

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

json2

The first patch to apply, as JSON. You can pass more patches as further arguments; they are applied from left to right.

Return type

JSON

Examples

The following SQL query replaces the value of b and adds the key c.

SELECT json_merge_patch( '{"a": 1, "b": 2}', '{"b": 3, "c": 4}' ) AS result

The query returns the following result.

result

{"a":1,"b":3,"c":4}

The following SQL query removes the key b, because the patch sets it to the JSON null.

SELECT json_merge_patch( '{"a": 1, "b": 2}', '{"b": null}' ) AS result

The query returns the following result.

result

{"a":1}

The following SQL query merges the nested object a key by key, but replaces the array tags as a whole.

SELECT json_merge_patch( '{"a": {"x": 1, "y": 2}, "tags": [1, 2]}', '{"a": {"y": 3}, "tags": [3]}' ) AS result

The query returns the following result.

result

{"a":{"x":1,"y":3},"tags":[3]}

The following SQL query applies two patches from left to right. Each patch adds one key to the document.

SELECT json_merge_patch( '{"a": 1}', '{"b": 2}', '{"c": 3}' ) AS result

The query returns the following result.

result

{"a":1,"b":2,"c":3}

Considerations

A merge patch cannot set a key to the JSON null, because null in a patch removes the key.

Arrays are not merged. A patch replaces the whole array, so to add one element to an array, use the json_insert function with the position [#].

json_merge_patch_diff function

Returns a JSON merge patch (RFC 7386) that turns old into new, so that json_merge_patch(old, json_merge_patch_diff(old, new)) returns new. In the patch, a key that is only in old is set to the JSON null, and a key that is new or changed has its value from new. Nested objects are compared key by key. Arrays and other values that differ are copied from new as a whole.

When two objects hold the same data, the result is the empty object {}; the order of keys does not matter. When the two documents are arrays, the result is new as a whole, even when the arrays are equal. If old is NULL, the result is new. If new is NULL, the result is NULL.

Syntax

json_merge_patch_diff(old, new)

Arguments

The json_merge_patch_diff function takes the following arguments:

old

The original JSON document, as a JSON value or a VARCHAR value that holds JSON text.

new

The changed JSON document, as a JSON value or a VARCHAR value that holds JSON text.

Return type

JSON

Examples

The following SQL query returns a patch that removes the key b and adds the key c.

SELECT json_merge_patch_diff( '{"a": 1, "b": 2}', '{"a": 1, "c": 3}' ) AS result

The query returns the following result.

result

{"b":null,"c":3}

The following SQL query compares nested objects. The patch holds only the nested value that changed.

SELECT json_merge_patch_diff( '{"a": {"x": 1, "y": 2}}', '{"a": {"x": 1, "y": 5}}' ) AS result

The query returns the following result.

result

{"a":{"y":5}}

The following SQL query applies the patch to the original document with json_merge_patch, which returns the changed document.

SELECT json_merge_patch( '{"a": 1, "b": 2}', json_merge_patch_diff( '{"a": 1, "b": 2}', '{"a": 1, "c": 3}' ) ) AS result

The query returns the following result.

result

{"a":1,"c":3}

The following SQL query compares two documents that differ only in the order of their keys. The patch is empty.

SELECT json_merge_patch_diff( '{"b": 1, "a": 2}', '{"a": 2, "b": 1}' ) AS result

The query returns the following result.

result

{}

Considerations

A merge patch cannot set a key to the JSON null. When new has a key whose value is null, the patch sets that key to null. Applying the patch removes the key instead of keeping it with a null value.

json_remove function

Returns json without the value at path. For an object member, the function removes the key and its value. For an array element, the function removes the element, and the later elements move up one position. If nothing exists at path, the function returns the document unchanged. If any argument is NULL, the result is NULL.

Syntax

json_remove(json, path)

Arguments

The json_remove function takes the following arguments:

json

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

path

The location of the value to remove, as a VARCHAR in JSONPath syntax, such as '$.a.b' or '$.items[0]', or JSON Pointer syntax, such as '/a/b'.

Return type

JSON

Examples

The following SQL query removes the key a from an object.

SELECT json_remove('{"a": 1, "b": 2}', '$.a') AS result

The query returns the following result.

result

{"b":2}

The following SQL query removes a key from a nested object, and the first element from an array.

SELECT json_remove( '{"a": {"x": 1, "y": 2}}', '$.a.x' ) AS nested_key, json_remove('[1, 2, 3]', '$[0]') AS first_element

The query returns the following result.

nested_key first_element

{"a":{"y":2}}

[2,3]

The following SQL query names a key that does not exist. The document is returned unchanged.

SELECT json_remove('{"a": 1}', '$.c') AS result

The query returns the following result.

result

{"a":1}

The following SQL query removes two keys by nesting two calls.

SELECT json_remove( json_remove('{"a": 1, "b": 2, "c": 3}', '$.a'), '$.b' ) AS result

The query returns the following result.

result

{"c":3}

Considerations

The function takes one path. To remove several values, nest the calls, as in json_remove(json_remove(json, '$.a'), '$.b').

Paths with wildcards, such as '$.*', are not supported and fail the query.

json_replace function

Returns json with the value at path replaced by value, but only when a value already exists at path. If nothing exists there, the document is returned unchanged, and no key or element is added.

If any argument is NULL, the result is NULL. To set a value to the JSON null, pass the text 'null' as value.

To add values only where none exist, use the json_insert function. To add a value or change it in one call, use the json_set function.

Syntax

json_replace(json, path, value)

Arguments

The json_replace function takes the following arguments:

json

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

path

The location of the value to replace, as a VARCHAR in JSONPath syntax, such as '$.a.b' or '$.items[#-1]', or JSON Pointer syntax, such as '/a/b'.

value

The new value, as JSON. A VARCHAR value is read as JSON text, so '10' is a number and '"x"' is a string. To use a SQL value, convert it with to_json, for example to_json('x').

Return type

JSON

Examples

The following SQL query replaces the value of the key a.

SELECT json_replace( '{"a": 1, "b": 2}', '$.a', '10' ) AS result

The query returns the following result.

result

{"a":10,"b":2}

The following SQL query names the key c, which does not exist. The document is returned unchanged.

SELECT json_replace('{"a": 1}', '$.c', '10') AS result

The query returns the following result.

result

{"a":1}

The following SQL query replaces the second element of an array with a string. to_json turns the SQL string into a JSON string.

SELECT json_replace( '[1, 2, 3]', '$[1]', to_json('two') ) AS result

The query returns the following result.

result

[1,"two",3]

The following SQL query sets a value to the JSON null with the text 'null', and shows that a SQL NULL makes the whole result NULL.

SELECT json_replace( '{"a": 1}', '$.a', 'null' ) AS json_null, json_replace( '{"a": 1}', '$.a', NULL ) IS NULL AS sql_null_result

The query returns the following result.

json_null sql_null_result

{"a":null}

true

Considerations

A string such as 'x' is not valid JSON text, so passing it as value fails the query. Use to_json('x') or '"x"'.

Paths with wildcards, such as '$.*', are not supported and fail the query.

json_set function

Returns json with value written at path. If path already has a value, the function replaces it. If it does not, the function adds the value, and also adds any objects on the path that do not exist yet. To append an element to an array, use # as the index, as in '$[#]'.

The function returns json unchanged if path leads through a value that is not an object or an array. It also returns json unchanged if path names an array position more than one past the last element. A position just past the last element, such as '$[2]' for an array of two elements, appends the value. If any argument is NULL, the result is NULL. To add a value only when the path has none, use the json_insert function. To change a value only when it exists, use the json_replace function.

Syntax

json_set(json, path, value)

Arguments

The json_set function takes the following arguments:

json

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

path

The location to write the value at, as a VARCHAR JSON path, such as '$.a' or '$.a[0]'.

value

The value to write, as a JSON value. A string literal is read as JSON text, so '10' writes the number 10 and 'null' writes JSON null. To write a string, use the to_json function, as in to_json('x'), or write the JSON text with its double quotes, as in '"x"'.

Return type

JSON

Examples

The following SQL query replaces the value of an existing member.

SELECT json_set('{"a": 1, "b": 2}', '$.a', '10') AS result

The query returns the following result.

result

{"a":10,"b":2}

The following SQL query writes to a path that does not exist yet. The function adds the object x and its member y.

SELECT json_set('{"a": {"b": 1}}', '$.x.y', '2') AS result

The query returns the following result.

result

{"a":{"b":1},"x":{"y":2}}

The following SQL query writes a string. to_json turns the string into a JSON string value.

SELECT json_set('{"a": 1}', '$.b', to_json('x')) AS result

The query returns the following result.

result

{"a":1,"b":"x"}

The following SQL query appends an element to an array with the index #.

SELECT json_set('[1, 2, 3]', '$[#]', '4') AS result

The query returns the following result.

result

[1,2,3,4]

Considerations

value must be a JSON value or VARCHAR JSON text. A string that is not JSON text, such as 'x', makes the query fail. A number such as 10 does not match the argument types either, so write it as '10' or to_json(10).

json_strip_nulls function

Returns json with every object member whose value is JSON null removed. The function works through nested objects too, including objects inside arrays. It does not remove null elements from arrays, and an object whose members are all removed stays in the result as {}.

If json is NULL, the result is NULL. Text that is not valid JSON makes the query fail.

Syntax

json_strip_nulls(json)

Arguments

The json_strip_nulls function takes the following argument:

json

The document to remove the members from, as a JSON value or VARCHAR JSON text.

Return type

JSON

Examples

The following SQL query removes the null members at the top level and in a nested object.

SELECT json_strip_nulls( '{"a": 1, "b": null, "c": {"d": null, "e": 2}}' ) AS result

The query returns the following result.

result

{"a":1,"c":{"e":2}}

The following SQL query shows that a null array element is kept, while the null member of the object in the array is removed.

SELECT json_strip_nulls('[1, null, {"a": null}]') AS result

The query returns the following result.

result

[1,null,{}]

The following SQL query converts a struct to JSON with to_json, and then removes the field that is NULL.

SELECT json_strip_nulls( to_json({'id': 1, 'email': NULL}) ) AS result

The query returns the following result.

result

{"id":1}