- Parse and stringify JSON5 with
Bun.JSON5.parseandBun.JSON5.stringify import&requireJSON5 files as modules at runtime (including hot reloading & watch mode support)import&requireJSON5 files in frontend apps via Bun’s bundler
Conformance
Bun’s JSON5 parser passes 100% of the official JSON5 test suite. The parser is written in Zig for optimal performance. You can view our translated test suite to see every test case.Runtime API
Bun.JSON5.parse()
Parse a JSON5 string into a JavaScript value.
Supported JSON5 Features
JSON5 is a superset of JSON based on ECMAScript 5.1 syntax. It supports:- Comments: single-line (
//) and multi-line (/* */) - Trailing commas: in objects and arrays
- Unquoted keys: valid ECMAScript 5.1 identifiers can be used as keys
- Single-quoted strings: in addition to double-quoted strings
- Multi-line strings: using backslash line continuations
- Hex numbers:
0xFF - Leading & trailing decimal points:
.5and5. - Infinity and NaN: positive and negative
- Explicit plus sign:
+42
Error Handling
Bun.JSON5.parse() throws a SyntaxError if the input is invalid JSON5:
Bun.JSON5.stringify()
Stringify a JavaScript value to a JSON5 string.
Pretty Printing
Pass aspace argument to format the output with indentation:
space argument can be a number (number of spaces) or a string (used as the indent character):
Special Values
UnlikeJSON.stringify, JSON5.stringify preserves special numeric values:
Module Import
ES Modules
You can import JSON5 files directly as ES modules:config.json5
Default Import
Named Imports
You can destructure top-level properties as named imports:CommonJS
JSON5 files can also be required in CommonJS:Hot Reloading with JSON5
When you run your application withbun --hot, changes to JSON5 files are automatically detected and reloaded:
config.json5
terminal
Bundler Integration
When you import JSON5 files and bundle with Bun, the JSON5 is parsed at build time and included as a JavaScript module:terminal
- Zero runtime JSON5 parsing overhead in production
- Smaller bundle sizes
- Tree-shaking support for unused properties (named imports)