JSON
Overview
This code block demonstrates how to query JSON file(s).
Foreign data wrapper name. Can be any string.
Foreign server name. Can be any string.
Foreign table name. Can be any string.
The path of a single JSON file or multiple JSON files.
For instance, s3://bucket/folder/file.json
if the file is in Amazon S3 or /path/to/file.json
if the file is on the local file system.
JSON Options
There are a number of options that can be passed into the CREATE FOREIGN TABLE
statement.
These are the same options accepted
by DuckDB’s read_json
function.
Enables auto detection of key names and value types.
Specifies key names and value types in the JSON file (e.g. {key1: 'INTEGER', key2: 'VARCHAR'}
). If auto_detect
is enabled the value of this setting will be inferred from the JSON file contents.
The compression type for the file. By default this will be detected automatically from the file extension (e.g., t.json.gz
will use gzip
, t.json
will use none
). Options are uncompressed
, gzip
, zstd
, and auto_detect
.
Whether strings representing integer values should be converted to a numerical type.
Specifies the date format to use when parsing dates. See Date Format
Whether or not an extra filename column should be included in the result.
Can be one of auto
, unstructured
, newline_delimited
and array
Whether or not to interpret the path as a Hive partitioned path.
Whether to ignore parse errors (only possible when format is newline_delimited
)
Maximum nesting depth to which the automatic schema detection detects types. Set to -1
to fully detect nested JSON types.
The maximum size of a JSON object (in bytes).
Determines whether the fields of JSON object will be unpacked into individual columns.
Can be one of auto
, true
or false
Suppose we have a JSON file with these contents:
Reading it with records
set to true
will result in these table contents:
Reading it with records
set to false
will result in these table contents:
If set to auto
DuckDB will try to determine the desired behaviour. See DuckDB documentation for more details.
Option to define number of sample objects for automatic JSON type detection. Set to -1
to scan the entire input file
Specifies the date format to use when parsing timestamps. See Date Format
Whether the schema’s of multiple JSON files should be unified.
Multiple JSON Files
To treat multiple JSON files as a single table, their paths should be passed in as a comma-separated string.
To treat a directory of JSON files as a single table, the glob pattern should be used.
The glob pattern can also be used to read all JSON files from multiple directories.
Cloud Object Stores
The object stores documentation explains how to provide secrets and other credentials for JSON files stored in object stores like S3.