# Import Attributes

> The import attributes JS syntax passes additional information about the module being imported, such as its type.

## How do I import JSON in JavaScript with import attributes?

The syntax that adds the `with` clause to `import` statements, for example `with { type: 'json' }`. Tells the runtime how to treat the imported module, allowing JSON to be imported as a module safely.

- Topic: Syntax
- Type: syntax
- Browser support: Chrome 123+, Chrome Android 123+, Edge 123+, Firefox 138+, Firefox Android 138+, Safari 17.2+, Safari iOS 17.2+

## Example

Wrong:

```js
import config from './config.json' assert { type: 'json' };
console.log(config.theme);
```

Right:

```js
import config from './config.json' with { type: 'json' };
console.log(config.theme);
```

Import attributes use the `with` keyword; the older `assert` form is not accepted by browsers that support this feature.

## Common mistakes

- Writing `import { name } from './data.json'`, which fails because JSON modules have only a default export.

## Key points

- The import uses `with { type: 'json' }`, not the old `assert` keyword.
- JSON data is read from the default import, not from named imports.
- Dynamic imports pass the attributes as `import(url, { with: { type: 'json' } })`.

## Interview questions

### Why do I need type: 'json' to import JSON?

The attribute tells the browser to treat the file as JSON. Without it, the import fails, which stops a server from swapping data for code that runs.

### Do import attributes work with dynamic import()?

Yes. Pass them as the second argument: `await import('./data.json', { with: { type: 'json' } })`, then read the data from `module.default`.

## Related cards

- [import / export](https://wonderium.dev/js/import-export.md)
- [JSON](https://wonderium.dev/js/json.md)
- [`<script type="importmap">`](https://wonderium.dev/html/import-map.md)
- [Top-level await](https://wonderium.dev/js/top-level-await.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/import-attributes
- Last updated: 2026-11-25
