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. |
Parses text as JSON and returns it in compact form. |
|
Describes the path syntax that JSON functions and operators use to select part of a JSON value. |
|
Builds a JSON array from a list of values. |
|
Returns the number of elements in a JSON array. |
|
Tests whether a JSON value contains another JSON value. |
|
Merges JSON objects at every depth, with later values taking precedence. |
|
Converts the JSON form of a parsed |
|
Returns one row for each element of a JSON array or each member of a JSON object. |
|
Tests whether a path selects a value in a JSON document. |
|
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. |
Returns the value at a path in a JSON document as text. |
|
Aggregates the values in a group into a JSON array. |
|
Aggregates key and value pairs in a group into a JSON object. |
|
Describes the keys and value types found across the JSON values in a group. |
|
Adds a value to a JSON document at a path where no value exists yet. |
|
Lists the key names of a JSON object. |
|
Merges JSON documents by applying each later document as a JSON merge patch. |
|
Returns the JSON merge patch that turns one JSON document into another. |
|
Returns a JSON document as compact text with sorted keys. |
|
Returns a JSON object whose members come from pairs of key and value arguments. |
|
Formats a JSON document on several indented lines for reading. |
|
json_quote |
Alias of to_json. |
Removes the value at a path from a JSON document. |
|
Replaces a value that already exists at a path in a JSON document. |
|
Parses SQL |
|
Writes a value at a JSON path, replacing the value that is there or adding it. |
|
Removes the object members whose value is JSON |
|
Returns the structure of a JSON value, with the type of each member and element. |
|
Converts JSON to a SQL value of the type that a structure describes, and returns |
|
Converts JSON to a SQL value of the type that a structure describes, and fails when a value is missing or does not convert. |
|
Returns one row for each value in a JSON document, including the values nested at every level. |
|
Returns the type of a JSON value, or of the value at a JSON path. |
|
Tests whether a string is valid JSON text. |
|
Returns the scalar value at a JSON path as JSON text. |
|
Converts a struct, such as a whole row, to a JSON object. |
|
Converts a value of any type to JSON. |