mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
## Summary - Adds `Bun.JSON5.parse()` and `Bun.JSON5.stringify()` as built-in APIs - Adds `.json5` file support in the module resolver and bundler - Parser uses a scanner/parser split architecture with a labeled switch pattern (like the YAML parser) — the scanner produces typed tokens, the parser never touches source bytes directly - 430+ tests covering the official JSON5 test suite, escape sequences, numbers, comments, whitespace (including all Unicode whitespace types), unquoted/reserved-word keys, unicode identifiers, deeply nested structures, garbage input, error messages, and stringify behavior <img width="659" height="610" alt="Screenshot 2026-01-25 at 12 19 57 AM" src="https://github.com/user-attachments/assets/e300125a-f197-4cad-90ed-e867b6232a01" /> ## Test plan - [x] `bun bd test test/js/bun/json5/json5.test.ts` — 317 tests - [x] `bun bd test test/js/bun/json5/json5-test-suite.test.ts` — 113 tests from the official JSON5 test suite - [x] `bun bd test test/js/bun/resolve/json5/json5.test.js` — .json5 module resolution closes #3175 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
75 lines
1.3 KiB
Plaintext
75 lines
1.3 KiB
Plaintext
---
|
|
title: Import a JSON5 file
|
|
sidebarTitle: Import JSON5
|
|
mode: center
|
|
---
|
|
|
|
Bun natively supports `.json5` imports.
|
|
|
|
```json5 config.json5 icon="file-code"
|
|
{
|
|
// Comments are allowed
|
|
database: {
|
|
host: "localhost",
|
|
port: 5432,
|
|
name: "myapp",
|
|
},
|
|
|
|
server: {
|
|
port: 3000,
|
|
timeout: 30,
|
|
},
|
|
|
|
features: {
|
|
auth: true,
|
|
rateLimit: true,
|
|
},
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
Import the file like any other source file.
|
|
|
|
```ts config.ts icon="/icons/typescript.svg"
|
|
import config from "./config.json5";
|
|
|
|
config.database.host; // => "localhost"
|
|
config.server.port; // => 3000
|
|
config.features.auth; // => true
|
|
```
|
|
|
|
---
|
|
|
|
You can also use named imports to destructure top-level properties:
|
|
|
|
```ts config.ts icon="/icons/typescript.svg"
|
|
import { database, server, features } from "./config.json5";
|
|
|
|
console.log(database.name); // => "myapp"
|
|
console.log(server.timeout); // => 30
|
|
console.log(features.rateLimit); // => true
|
|
```
|
|
|
|
---
|
|
|
|
For parsing JSON5 strings at runtime, use `Bun.JSON5.parse()`:
|
|
|
|
```ts config.ts icon="/icons/typescript.svg"
|
|
const data = JSON5.parse(`{
|
|
name: 'John Doe',
|
|
age: 30,
|
|
hobbies: [
|
|
'reading',
|
|
'coding',
|
|
],
|
|
}`);
|
|
|
|
console.log(data.name); // => "John Doe"
|
|
console.log(data.hobbies); // => ["reading", "coding"]
|
|
```
|
|
|
|
---
|
|
|
|
See [Docs > API > JSON5](/runtime/json5) for complete documentation on JSON5 support in Bun.
|