> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to turn an array of objects in a JSON response into table rows

> Read a JSON document whose records are nested in an array of objects, such as a REST API response, and expand the array into one table row per object with ARRAY JOIN.

A REST API often returns a single JSON document in which the records are nested in an array, for example under a `results` key. Read the document in the `JSONEachRow` format with a table function such as [`url`](https://clickhouse.com/docs/reference/functions/table-functions/url) or [`file`](https://clickhouse.com/docs/reference/functions/table-functions/file), and expand the array with [`ARRAY JOIN`](https://clickhouse.com/docs/reference/statements/select/array-join): every object of the array becomes a row, and its keys become columns.

## Example

Save this document as `response.json`:

```json theme={null}
{
    "status": 200,
    "request_id": "req-0001",
    "results": [
        {"id": 1, "name": "apple", "price": 1.5},
        {"id": 2, "name": "banana", "price": 0.25},
        {"id": 3, "name": "cherry", "price": 12}
    ]
}
```

Schema inference reads the whole document as one row and the `results` array as an array of named tuples:

```sql theme={null}
DESCRIBE file('response.json', 'JSONEachRow')
SETTINGS describe_compact_output = 1;
```

```text theme={null}
   ┌─name───────┬─type───────────────────────────────────────────────────────────────────────────────────────┐
1. │ status     │ Nullable(Int64)                                                                            │
2. │ request_id │ Nullable(String)                                                                           │
3. │ results    │ Array(Nullable(Tuple(id Nullable(Int64), name Nullable(String), price Nullable(Float64)))) │
   └────────────┴────────────────────────────────────────────────────────────────────────────────────────────┘
```

`ARRAY JOIN results` produces one row per element of the array. Inside the query, `results.id` is the `id` key of the current element. Columns outside the array, such as `request_id`, are repeated on every row:

```sql theme={null}
SELECT
    request_id,
    results.id AS id,
    results.name AS name,
    results.price AS price
FROM file('response.json', 'JSONEachRow')
ARRAY JOIN results;
```

```text theme={null}
   ┌─request_id─┬─id─┬─name───┬─price─┐
1. │ req-0001   │  1 │ apple  │   1.5 │
2. │ req-0001   │  2 │ banana │  0.25 │
3. │ req-0001   │  3 │ cherry │    12 │
   └────────────┴────┴────────┴───────┘
```

## Read the response from an HTTP endpoint

The same query works on a live response. Replace `file` with the `url` table function, and pass request headers, such as an authorization token, in `headers(...)`:

```sql theme={null}
SELECT
    results.id AS id,
    results.name AS name,
    results.price AS price
FROM url(
    'https://example.com/api/products',
    'JSONEachRow',
    headers('Authorization' = 'Bearer <token>'))
ARRAY JOIN results;
```

To query the endpoint repeatedly, create a table with the [`URL`](https://clickhouse.com/docs/reference/engines/table-engines/special/url) table engine and run the same `SELECT ... ARRAY JOIN results` on it. Every `SELECT` sends a new request:

```sql theme={null}
CREATE TABLE api_products
(
    results Array(Tuple(id UInt32, name String, price Decimal(10, 2)))
)
ENGINE = URL('https://example.com/api/products', 'JSONEachRow', headers('Authorization' = 'Bearer <token>'));
```

## Insert the rows into a table

Use `INSERT ... SELECT` with the same `ARRAY JOIN`. Pass the structure of the array as the third argument of the table function, so that the values are read with the types of your table instead of the inferred `Nullable` types:

```sql theme={null}
CREATE TABLE products
(
    id UInt32,
    name String,
    price Decimal(10, 2)
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO products
SELECT
    results.id,
    results.name,
    results.price
FROM file(
    'response.json',
    'JSONEachRow',
    'results Array(Tuple(id UInt32, name String, price Decimal(10, 2)))')
ARRAY JOIN results;

SELECT * FROM products ORDER BY id;
```

```text theme={null}
   ┌─id─┬─name───┬─price─┐
1. │  1 │ apple  │   1.5 │
2. │  2 │ banana │  0.25 │
3. │  3 │ cherry │    12 │
   └────┴────────┴───────┘
```

Without the explicit structure, this `INSERT` fails:

```text theme={null}
Code: 47. DB::Exception: Unknown expression identifier `results`. In scope SELECT results.id, results.name, results.price FROM file('response.json', 'JSONEachRow') ARRAY JOIN results. (UNKNOWN_IDENTIFIER)
```

When the `SELECT` list of an `INSERT ... SELECT` contains only column names, ClickHouse does not infer the schema from the data. It builds the structure of the table function from the columns of the target table, so the file is read as if it had top-level columns `results.id`, `results.name` and `results.price`, and there is no `results` column to expand. Either pass the structure as shown above, or add `SETTINGS use_structure_from_insertion_table_in_table_functions = 0` to the query. See [Using structure from insertion table](https://clickhouse.com/docs/concepts/features/operations/insert/schema-inference.md#using-structure-from-insertion-table).

## Things to know

* A key that is missing in some of the objects is returned as `NULL` with the inferred schema.
* `ARRAY JOIN` returns no row for a document whose array is empty. Use `LEFT ARRAY JOIN` to keep such a document as one row.
* If the array is nested deeper, give the full path and an alias: `ARRAY JOIN data.results AS r`, then select `r.id`, `r.name`.
* If the response is an array at the top level, such as `[{"id": 1}, {"id": 2}]`, `JSONEachRow` already returns one row per object and you do not need `ARRAY JOIN`.
* If the objects do not share a fixed set of keys, declare the column as an array of the [`JSON`](https://clickhouse.com/docs/reference/data-types/newjson) type: pass `'results Array(JSON)'` as the structure. With `ARRAY JOIN results AS r`, `r.id` then has the `Dynamic` type; cast it where you need a concrete type, for example `r.id::UInt32`.

## Related pages

* [`ARRAY JOIN` clause](https://clickhouse.com/docs/reference/statements/select/array-join)
* [`url` table function](https://clickhouse.com/docs/reference/functions/table-functions/url)
* [Schema inference for JSON formats](https://clickhouse.com/docs/concepts/features/operations/insert/schema-inference.md#json-formats)
* [Importing and querying JSON array objects](https://clickhouse.com/docs/resources/support-center/knowledge-base/data-import-export/importing-and-working-with-json-array-objects.md)
