Your config has comments and trailing commas. Go's JSON package won't read it.
encoding/json speaks plain JSON only: comments, trailing commas, single-quoted strings and unquoted keys all fail. In the official JSON5 test corpus, yosuke-furukawa/json5 v0.1.1 parses 30 of the 83 valid files, failing every corpus case that uses a block comment or a single-quoted string. So your hand-editable config and fixture files don't parse.
json5 is a spec-true JSON5 parser and encoder for Go with the encoding/json API you already know.
var cfg Config
err := json5.Unmarshal([]byte(`{
// tune the worker pool
workers: 0x10, // hex is fine
timeout: .5, // so is a leading dot
tags: ['fast', 'cheap',],
}`), &cfg)go get github.com/ofershap/json5Standard library only. No dependencies.
- Parses the full JSON5 spec: comments (line and block), single-quoted strings, unquoted and Unicode identifier keys, hex / leading-dot / trailing-dot numbers,
NaNandInfinity, trailing commas, multi-line strings, every escape form. - Drops in for
encoding/json:Unmarshal,Marshal,MarshalIndent,Valid,Decoder,Encoder, andjsonstruct tags behave the way you expect.Decoder.UseNumber()preserves big integers. - Marshals strict JSON by default, so output interoperates everywhere.
MarshalStyledopts into JSON5 style: indentation, single quotes, unquoted keys, trailing commas. - Streams:
Decoderreads multiple documents from one stream; errors carry line, column and byte offset. - Round-trips NaN and Infinity to their JSON5 literals instead of erroring like
encoding/json.
- Comment preservation: comments are parsed and skipped, not kept for re-emission. Round-tripping a file loses its comments.
- No leniency flags: parsing is spec-strict. An LLM's
{"a": 1}output is already valid JSON5; output with markdown fences or prose around it is a job for your extractor, not this parser. - NaN/Infinity into typed structs via
Unmarshalerrors by design; decode intofloat64oranyfields when you expect them.
Every compliance claim below runs in CI on every push:
- Official test corpus: 83/83 valid files from json5/json5-tests parse to the exact values the reference implementation (
json5on npm) computes, and 7/7 invalid inputs are rejected. The corpus is vendored undertestdata/(MIT, attribution included). - Differential testing: 512 generated documents - random structures, comment injections, edge numbers, Unicode strings - parse to identical accept/reject decisions and identical values as the reference implementation. 0 mismatches.
- Fuzzed: the fuzz seed corpus plus 860k+ generated inputs without a panic.
The gap this fills is measurable: running the same corpus against github.com/yosuke-furukawa/json5 v0.1.1 (the most-starred Go port, last touched 2023) passes 30 of the 83 valid files. Comments, single-quoted strings and form-feed whitespace all fail there (open issues).
JSON5 is a config format; correctness comes first. A realistic config document parses in ~6.5µs on a laptop (see bench_test.go). If you benchmark a hot path, open an issue with numbers.
How do I parse JSON5 in Go?
go get github.com/ofershap/json5, then json5.Unmarshal(data, &v) exactly like encoding/json, including struct tags. Use json5.NewDecoder(r) for streams and UseNumber() to keep big integers exact.What is the difference between JSON and JSON5?
JSON5 is a superset of JSON that allows comments, single-quoted strings, unquoted object keys, trailing commas, hex and leading/trailing-dot numbers,NaN/Infinity, and multi-line strings. Every valid JSON document is valid JSON5, so this parser reads plain JSON too.Can Go's encoding/json read JSON5?
No.encoding/json implements RFC 8259 strictly and rejects comments, single quotes, unquoted keys and trailing commas by design. You need a JSON5-specific parser like this one.Is json5-go / yosuke-furukawa/json5 maintained?
As of November 2026, no Go JSON5 port with a passing spec corpus is maintained: yosuke-furukawa/json5 (158 stars) was last released in 2023 and fails most of the official corpus; titanous/json5 and json5/json5-go are older and unmaintained. This repository runs the full official corpus in CI so the claim stays checkable.Can I use JSON5 for config files in Go?
Yes - that is the primary use case. Write configs with comments and trailing commas, decode into structs withjson tags, and marshal back to strict JSON with json5.Marshal when another tool needs plain JSON.Does it handle LLM output that is "almost JSON"?
Only when the output is syntactically valid JSON5: comments, trailing commas, single quotes and unquoted keys are legal JSON5 and parse fine. It is not a repair tool - truncated documents, missing quotes, markdown fences or surrounding prose are not JSON5, and extracting or repairing those is a separate job.Issues and PRs welcome - see CONTRIBUTING.md. Please report security issues privately per SECURITY.md.
More projects: gitshow · real-browser-mcp · cursor-usage-tracker · mcp-server-markdown · readme-builder · grok-bot-skills · mashov-parent-brief · gailwind · lather
README built with README Builder
