skills/bun-runtime-json5/SKILL.md
Use Bun's built-in support for JSON5 files through both runtime APIs and bundler integration
npx skillsauth add jarle/bun-skills Bun JSON5Install this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Use Bun's built-in support for JSON5 files through both runtime APIs and bundler integration
In Bun, JSON5 is a first-class citizen alongside JSON, TOML, and YAML. You can:
Bun.JSON5.parse and Bun.JSON5.stringifyimport & require JSON5 files as modules at runtime (including hot reloading & watch mode support)import & require JSON5 files in frontend apps via Bun's bundlerBun'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.
Bun.JSON5.parse()Parse a JSON5 string into a JavaScript value.
import { JSON5 } from "bun";
const data = JSON5.parse(`{
// JSON5 supports comments
name: 'my-app',
version: '1.0.0',
debug: true,
// trailing commas are allowed
tags: ['web', 'api',],
}`);
console.log(data);
// {
// name: "my-app",
// version: "1.0.0",
// debug: true,
// tags: ["web", "api"]
// }
JSON5 is a superset of JSON based on ECMAScript 5.1 syntax. It supports:
//) and multi-line (/* */)0xFF.5 and 5.+42const data = JSON5.parse(`{
// Unquoted keys
unquoted: 'keys work',
// Single and double quotes
single: 'single-quoted',
double: "double-quoted",
// Trailing commas
trailing: 'comma',
// Special numbers
hex: 0xDEADbeef,
half: .5,
to: Infinity,
nan: NaN,
// Multi-line strings
multiline: 'line 1 \
line 2',
}`);
Bun.JSON5.parse() throws a SyntaxError if the input is invalid JSON5:
try {
JSON5.parse("{invalid}");
} catch (error) {
console.error("Failed to parse JSON5:", error.message);
}
Bun.JSON5.stringify()Stringify a JavaScript value to a JSON5 string.
import { JSON5 } from "bun";
const str = JSON5.stringify({ name: "my-app", version: "1.0.0" });
console.log(str);
// {name:'my-app',version:'1.0.0'}
Pass a space argument to format the output with indentation:
const pretty = JSON5.stringify(
{
name: "my-app",
debug: true,
tags: ["web", "api"],
},
null,
2,
);
console.log(pretty);
// {
// name: 'my-app',
// debug: true,
// tags: [
// 'web',
// 'api',
// ],
// }
The space argument can be a number (number of spaces) or a string (used as the indent character):
// Tab indentation
JSON5.stringify(data, null, "\t");
Unlike JSON.stringify, JSON5.stringify preserves special numeric values:
JSON5.stringify({ inf: Infinity, ninf: -Infinity, nan: NaN });
// {inf:Infinity,ninf:-Infinity,nan:NaN}
You can import JSON5 files directly as ES modules:
{
// Database configuration
database: {
host: "localhost",
port: 5432,
name: "myapp",
},
features: {
auth: true,
rateLimit: true,
analytics: false,
},
}
import config from "./config.json5";
console.log(config.database.host); // "localhost"
console.log(config.features.auth); // true
You can destructure top-level properties as named imports:
import { database, features } from "./config.json5";
console.log(database.host); // "localhost"
console.log(features.rateLimit); // true
JSON5 files can also be required in CommonJS:
const config = require("./config.json5");
console.log(config.database.name); // "myapp"
// Destructuring also works
const { database, features } = require("./config.json5");
When you run your application with bun --hot, changes to JSON5 files are automatically detected and reloaded:
{
server: {
port: 3000,
host: "localhost",
},
features: {
debug: true,
verbose: false,
},
}
import { server, features } from "./config.json5";
Bun.serve({
port: server.port,
hostname: server.host,
fetch(req) {
if (features.verbose) {
console.log(`${req.method} ${req.url}`);
}
return new Response("Hello World");
},
});
Run with hot reloading:
bun --hot server.ts
When you import JSON5 files and bundle with Bun, the JSON5 is parsed at build time and included as a JavaScript module:
bun build app.ts --outdir=dist
This means:
JSON5 files can be dynamically imported:
const config = await import("./config.json5");
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling