> For AI agents: the complete documentation index is available at https://rspack.rs/zh/llms.txt, the full documentation bundle is available at https://rspack.rs/zh/llms-full.txt.

# JSON

Rspack 内置了对 [JSON](https://en.wikipedia.org/wiki/JSON) 的支持，你可以直接导入 JSON 文件。

## 默认导入

```json title="example.json"
{
  "foo": "bar"
}
```

```ts title="index.js"
import json from './example.json';
console.log(json.foo); // "bar"
```

## 具名导入

在非 `.mjs` 的非严格 ESM 文件中，你可以直接导入 JSON 中的属性。

```json title="example.json"
{
  "foo": "bar"
}
```

```ts title="index.js"
import { foo } from './example.json';
console.log(foo); // "bar"
```

## 使用 import attributes

Rspack 支持 [import attributes](https://github.com/tc39/proposal-import-attributes)，你可以通过 import attributes 来引入 JSON：

```ts title="index.js"
import json from './example.json' with { type: 'json' };
import('./example.json', { with: { type: 'json' } });
```
