View a markdown version of this page

JSON functions and operators in DuckDB - Amazon Athena

JSON functions and operators in DuckDB

Use JSON functions and operators to parse JSON text, read values from JSON documents, and test what a document contains. You can also build and merge JSON values, and convert JSON to SQL types such as STRUCT and lists.

DuckDB stores JSON in the JSON type. The JSON functions also accept JSON text as a VARCHAR value and parse it for you, so json_extract('{"a": 1}', '$.a') works without a cast. If the text is not valid JSON, the query fails. To test text before you parse it, use the json_valid function.

A SQL NULL and the JSON value null are not the same. Functions that return JSON, such as json_extract and its operator form ->, return the JSON value null for a member whose value is null. For that value, IS NULL is false. Functions that return text, such as the ->> operator, return SQL NULL instead. Both kinds of function return SQL NULL when the member does not exist. To test the result of the -> operator, put the expression in parentheses, as in (json -> '$.a') IS NULL.

Functions that read part of a document take a JSON path, such as '$.customer.name' or '$.items[0]'. For more information about the path syntax, see JSON paths.

The following table lists the JSON functions and operators, with a link to the description of each one.

Function or operator Description

->>

Extracts a value from JSON at a path or an array position and returns it as text.

from_json

Alias of json_transform.

from_json_strict

Alias of json_transform_strict.

json

Parses text as JSON and returns it in compact form.

JSON paths

Describes the path syntax that JSON functions and operators use to select part of a JSON value.

json_array

Builds a JSON array from a list of values.

json_array_length

Returns the number of elements in a JSON array.

json_contains

Tests whether a JSON value contains another JSON value.

json_deep_merge

Merges JSON objects at every depth, with later values taking precedence.

json_deserialize_sql

Converts the JSON form of a parsed SELECT statement back to SQL text.

json_each

Returns one row for each element of a JSON array or each member of a JSON object.

json_exists

Tests whether a path selects a value in a JSON document.

json_extract

Extracts a value from JSON at a path or an array position and returns it as JSON.

json_extract_path

Alias of json_extract.

json_extract_path_text

Alias of json_extract_string.

json_extract_string

Returns the value at a path in a JSON document as text.

json_group_array

Aggregates the values in a group into a JSON array.

json_group_object

Aggregates key and value pairs in a group into a JSON object.

json_group_structure

Describes the keys and value types found across the JSON values in a group.

json_insert

Adds a value to a JSON document at a path where no value exists yet.

json_keys

Lists the key names of a JSON object.

json_merge_patch

Merges JSON documents by applying each later document as a JSON merge patch.

json_merge_patch_diff

Returns the JSON merge patch that turns one JSON document into another.

json_normalize

Returns a JSON document as compact text with sorted keys.

json_object

Returns a JSON object whose members come from pairs of key and value arguments.

json_pretty

Formats a JSON document on several indented lines for reading.

json_quote

Alias of to_json.

json_remove

Removes the value at a path from a JSON document.

json_replace

Replaces a value that already exists at a path in a JSON document.

json_serialize_sql

Parses SQL SELECT text and returns its parse tree as JSON.

json_set

Writes a value at a JSON path, replacing the value that is there or adding it.

json_strip_nulls

Removes the object members whose value is JSON null, at every level of a JSON value.

json_structure

Returns the structure of a JSON value, with the type of each member and element.

json_transform

Converts JSON to a SQL value of the type that a structure describes, and returns NULL for values that are missing or do not convert.

json_transform_strict

Converts JSON to a SQL value of the type that a structure describes, and fails when a value is missing or does not convert.

json_tree

Returns one row for each value in a JSON document, including the values nested at every level.

json_type

Returns the type of a JSON value, or of the value at a JSON path.

json_valid

Tests whether a string is valid JSON text.

json_value

Returns the scalar value at a JSON path as JSON text.

row_to_json

Converts a struct, such as a whole row, to a JSON object.

to_json

Converts a value of any type to JSON.