Repair invalid JSON strings in Go. Especially handy for cleaning up JSON coming from Large Language Models (LLMs) or hand-written configs.
English | 中文
- Fix missing/wrong quotes on keys and strings
- Convert single quotes to double quotes
- Add missing commas, closing braces/brackets
- Remove trailing commas
- Normalize booleans (
True/False➜true/false) and nulls (None➜null) - Strip comments (
//,/* */,#) - Light escaping and number parsing fixes
- Zero external deps (stdlib only)
go get github.com/lxw665/json_repair_goNote: make sure your import path matches the repository path you publish under. If you fork, replace lxw665 with your GitHub username.
package main
import (
"fmt"
"log"
jsonrepair "github.com/lxw665/json_repair_go"
)
func main() {
broken := `{name: 'John', age: 30, active: True`
// 1) Repair to JSON string
fixed, err := jsonrepair.RepairJSON(broken)
if err != nil { log.Fatal(err) }
fmt.Println(fixed) // {"name":"John","age":30,"active":true}
// 2) Parse directly to Go value
v, err := jsonrepair.Loads(broken)
if err != nil { log.Fatal(err) }
fmt.Printf("%+v\n", v)
}RepairJSON(jsonStr string) (string, error)— Repair and return a JSON stringRepairJSONWithOptions(jsonStr string, options RepairOptions) (string, error)Loads(jsonStr string) (interface{}, error)— Repair and parse to Go valuesUnmarshal(data []byte, v any) error— Drop-in forjson.Unmarshalwith repairLoad(r io.Reader, v any) error
Options:
type RepairOptions struct {
SkipValidation bool // Skip initial json.Unmarshal check
ReturnObjects bool // Return parsed objects instead of JSON string
Logging bool // Enable repair logs
StreamStable bool // Keep repair stable for streaming use-cases
EnsureASCII bool // Escape non-ASCII
}Input ➜ Output
{name: 'John', age: 30➜{"name":"John","age":30}{'key': 'value',}➜{"key":"value"}[1, 2, 3,➜[1,2,3]{"active": True}➜{"active":true}{"value": None}➜{"value":null}{a: 1, b: 2➜{"a":1,"b":2}
More runnable examples in demo/:
cd demo
go run .go test -v
go test -bench=.
go test -cover- Python: https://github.com/mangiucugna/json_repair
- TypeScript: https://github.com/josdejong/jsonrepair
- Rust: https://github.com/oramasearch/llm_json
- Ruby: https://github.com/sashazykov/json-repair-rb
PRs and issues are welcome.
- Fork ➜ 2) create branch ➜ 3) commit ➜ 4) push ➜ 5) open PR
MIT. See LICENSE.
If this project helps you, please give it a ⭐️!