Jsonrepair
A high-performance Golang library for easily repairing invalid JSON documents. Designed to fix common JSON issues and optimize JSON content generated by language models (LLMs).
Install / Use
/learn @kaptinlin/JsonrepairREADME
jsonrepair
Easily repair invalid JSON documents with the Go jsonrepair library. This library is a direct port of the popular jsonrepair JavaScript library, designed to address common issues found in JSON data. Leveraging the performance benefits of Go, it maintains compatibility and reliability with the original JavaScript library. It is particularly useful for fixing JSON content generated by language models (LLMs).
Features
The jsonrepair library can automatically fix the following JSON issues:
-
Add missing quotes around keys: Ensures all keys are properly quoted.
-
Add missing escape characters: Adds necessary escape characters where needed.
-
Add missing commas: Inserts missing commas between elements.
-
Add missing closing brackets: Closes any unclosed brackets.
-
Repair truncated JSON: Completes truncated JSON data.
-
Replace single quotes with double quotes: Converts single quotes to double quotes.
-
Replace special quote characters: Converts characters like
“...”to standard double quotes. -
Replace special white space characters: Converts special whitespace characters to regular spaces.
-
Replace Python constants: Converts
None,True,Falsetonull,true,false. -
Strip trailing commas: Removes any trailing commas.
-
Strip comments: Eliminates comments such as
/* ... */and// .... -
Strip fenced code blocks: Removes markdown fenced code blocks like
```jsonand```. -
Strip ellipsis: Removes ellipsis in arrays and objects, e.g.,
[1, 2, 3, ...]. -
Strip JSONP notation: Removes JSONP callbacks, e.g.,
callback({ ... }). -
Strip escape characters: Removes escape characters from strings, e.g.,
{\"stringified\": \"content\"}. -
Strip MongoDB data types: Converts types like
NumberLong(2)andISODate("2012-12-19T06:01:17.171Z")to standard JSON. -
Concatenate strings: Merges strings split across lines, e.g.,
"long text" + "more text on next line". -
Convert newline-delimited JSON: Encloses newline-delimited JSON in an array to make it valid, for example:
{ "id": 1, "name": "John" } { "id": 2, "name": "Sarah" }
Install
Install the library using go get:
go get github.com/kaptinlin/jsonrepair
Usage
Basic Usage
Use the Repair function to repair a JSON string:
package main
import (
"fmt"
"log"
"github.com/kaptinlin/jsonrepair"
)
func main() {
// The following is invalid JSON: it consists of JSON contents copied from
// a JavaScript code base, where the keys are missing double quotes,
// and strings are using single quotes:
input := "{name: 'John'}"
repaired, err := jsonrepair.Repair(input)
if err != nil {
log.Fatalf("Failed to repair JSON: %v", err)
}
fmt.Println(repaired) // Output: {"name": "John"}
}
API
Repair Function
// Repair attempts to repair the given JSON string and returns the repaired version.
// It returns an error if an issue is encountered which could not be solved.
func Repair(text string) (string, error)
Note: The previous
JSONRepairfunction is still available as a deprecated alias forRepair.
Error Handling
The library returns structured *jsonrepair.Error values with position information:
repaired, err := jsonrepair.Repair(input)
if err != nil {
var repairErr *jsonrepair.Error
if errors.As(err, &repairErr) {
fmt.Printf("Error: %s at position %d\n", repairErr.Message, repairErr.Position)
}
}
Predefined sentinel errors for use with errors.Is():
ErrUnexpectedEnd- unexpected end of JSON stringErrObjectKeyExpected- object key expectedErrColonExpected- colon expectedErrInvalidCharacter- invalid characterErrUnexpectedCharacter- unexpected characterErrInvalidUnicode- invalid unicode character
How to Contribute
Contributions to the jsonrepair package are welcome. If you'd like to contribute, please follow the contribution guidelines.
License
Released under the MIT license. See the LICENSE file for details.
Acknowledgements
This library is a Go port of the JavaScript library jsonrepair by Jos de Jong. The original logic and behavior have been closely followed to ensure compatibility and reliability. Special thanks to the original author for creating such a useful tool.
