G.4. Reading JSON Files (read_json) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 17.

Execute the following command on the Postgres Pro AXE server:

  SELECT * FROM read_json('path', optional_parameters);
  

Where:

  • path: The path to the JSON files that must be read.

    You can specify a value in one of the following formats:

    • a path to a single JSON file, such as 'file.json'

    • a glob pattern, such as 'data/*.json'

    • an array of paths, such as ['file1.json', 'file2.json']

  • optional_parameters: The optional parameters, specified in the parameter := value format.

    Available parameters:

    • compression: The file compression method.

      Possible values:

      • auto_detect

      • none

      • gzip

      • zstd

      Default value: auto_detect — the compression method is detected automatically using file extensions.

    • filename: Specifies whether to return the filename column with file paths. Since DuckDB v1.3.0, this column is added automatically.

      Possible values:

      • true

      • false

      Default value: false.

    • format: The JSON format.

      Possible values:

      • auto

      • unstructured

      • newline_delimited

      • array

      Default value: array.

    • hive_partitioning: Specifies whether the directory is associated with a partitioned analytical table and whether partition columns are returned.

      Possible values:

      • true

      • false

      Default value: Detected automatically based on the directory structure.

      For more information about Hive partitioning, refer to Section 15.2.

    • ignore_errors: Specifies whether to ignore parsing errors.

      Possible values:

      • true

      • false

      This parameter is applicable only if the format parameter is set to newline_delimited.

      Default value: false.

    • maximum_sample_files: The maximum number of JSON files sampled for automatic detection of column names and data types.

      You can specify the number of JSON objects sampled within each file manually using the sample_size parameter.

      Default value: 32.

    • maximum_object_size: The maximum size of a JSON object, in bytes.

      Default value: 16777216.

    • auto_detect: Specifies whether to automatically detect column names and data types.

      Possible values:

      • true

      • false

      Default value: true.

    • dateformat: The format used when parsing dates.

      Default value: iso.

    • maximum_depth: The maximum nesting depth for automatic detection of column names and data types.

      Default value: -1 — no nesting depth limit.

    • records: Specifies whether to return JSON objects as individual columns.

      Possible values:

      • auto

      • true

      • false

      If this parameter is set to false, each JSON object is returned as a STRUCT value.

      Default value: auto.

    • sample_size: The number of JSON objects sampled within each file for automatic detection of column names and data types.

      You can specify the number of sampled JSON files manually using the maximum_sample_files parameter.

      To scan JSON files entirely, set this parameter to -1.

      Default value: 20480.

    • timestampformat: The format used when parsing timestamps.

      Default value: iso.

    • union_by_name: Specifies whether columns from different files are aligned by name instead of by position.

      Possible values:

      • true

      • false

      If this parameter is set to true, memory consumption increases.

      Default value: false.

    • map_inference_threshold: The threshold at which automatic detection of column names and data types assigns the MAP type instead of the STRUCT type to a JSON object.

      To disable automatic assignment of the MAP type, set this parameter to -1.

      Default value: 200.

    • field_appearance_threshold: The threshold that determines whether the JSON reader assigns the MAP or the STRUCT data type to a JSON object.

      For each field, the JSON reader calculates how frequently the field appears relative to the number of objects in the sample. If the average of these ratios across all fields of an object is below this threshold, the reader defaults to assigning the MAP data type to this object.

      Default value: 0.1.

Postgres Pro AXE will return rows of JSON files.

Example G.6. Reading JSON Files

Contents of file.json:

  [
    {"id": 1, "name": "Alice", "age": 30, "hired": "2020-01-15"},
    {"id": 2, "name": "Bob", "age": 25, "hired": "2021-03-22"},
    {"id": 3, "name": "Charlie", "age": 35, "hired": "2019-07-01"}
  ]
  
  SELECT * FROM read_json('file.json');
  
   id |  name   | age |   hired
  ----+---------+-----+------------
  1 | Alice   | 30  | 2020-01-15
  2 | Bob     | 25  | 2021-03-22
  3 | Charlie | 35  | 2019-07-01
  

You can select specific columns by assigning an alias to a procedure, such as r, and using the r['column_name'] syntax:

  SELECT r['id']::INTEGER    AS id,
         r['name']::VARCHAR  AS name,
         r['age']::INTEGER   AS age,
         r['hired']::DATE    AS hired
  FROM read_json('file.json') AS r
  WHERE r['age']::INTEGER = 25;
  
   id | name | age |   hired
  ----+------+-----+------------
  2 | Bob  |  25 | 2021-03-22
  

FAQ