> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> JSONAsObject 格式文档

# JSONAsObject

<div id="description">
  ## 说明
</div>

在这种格式中，单个 JSON 对象 会被解释为一个 [JSON](/zh/reference/data-types/newjson) 值。如果输入包含多个 JSON 对象 (以逗号分隔) ，它们会被解释为不同的行。如果输入数据包含在 `[]` 中，则会被解释为 JSON 数组。

这种格式只能用于解析仅包含一个 [JSON](/zh/reference/data-types/newjson) 类型字段的表。其余列必须设置为 [`DEFAULT`](/zh/reference/statements/create/table#default) 或 [`MATERIALIZED`](/zh/reference/statements/create/view#materialized-view)。

<div id="example-usage">
  ## 示例用法
</div>

<div id="basic-example">
  ### 基本示例
</div>

```sql title="Query" theme={null}
CREATE TABLE json_as_object (json JSON) ENGINE = Memory;
INSERT INTO json_as_object (json) FORMAT JSONAsObject {"foo":{"bar":{"x":"y"},"baz":1}},{},{"any json stucture":1}
SELECT * FROM json_as_object FORMAT JSONEachRow;
```

```response title="Response" theme={null}
{"json":{"foo":{"bar":{"x":"y"},"baz":"1"}}}
{"json":{}}
{"json":{"any json stucture":"1"}}
```

<div id="an-array-of-json-objects">
  ### JSON 对象数组
</div>

```sql title="Query" theme={null}
CREATE TABLE json_square_brackets (field JSON) ENGINE = Memory;
INSERT INTO json_square_brackets FORMAT JSONAsObject [{"id": 1, "name": "name1"}, {"id": 2, "name": "name2"}];
SELECT * FROM json_square_brackets FORMAT JSONEachRow;
```

```response title="Response" theme={null}
{"field":{"id":"1","name":"name1"}}
{"field":{"id":"2","name":"name2"}}
```

<div id="columns-with-default-values">
  ### 带默认值的列
</div>

```sql title="Query" theme={null}
CREATE TABLE json_as_object (json JSON, time DateTime MATERIALIZED now()) ENGINE = Memory;
INSERT INTO json_as_object (json) FORMAT JSONAsObject {"foo":{"bar":{"x":"y"},"baz":1}};
INSERT INTO json_as_object (json) FORMAT JSONAsObject {};
INSERT INTO json_as_object (json) FORMAT JSONAsObject {"any json stucture":1}
SELECT time, json FROM json_as_object FORMAT JSONEachRow
```

```response title="Response" theme={null}
{"time":"2024-09-16 12:18:10","json":{}}
{"time":"2024-09-16 12:18:13","json":{"any json stucture":"1"}}
{"time":"2024-09-16 12:18:08","json":{"foo":{"bar":{"x":"y"},"baz":"1"}}}
```

<div id="format-settings">
  ## 格式设置
</div>
