Repair JSON!Solution for JSON Anomalies from LLMs.
Repair JSON!Solution for JSON Anomalies from LLMs.
Welcome to the json-repair, your go-to solution for fixing those pesky JSON anomalies that can sidetrack your Large Language Model (LLM) processes. Our toolkit is designed to be the Swiss Army knife for all your JSON repair needs.
"\n{"key": TRUE, "key2": FALSE, "key3": Null {'key': 'string', 'key2': false, \"key3\": null, \"key4\": unquoted}[1, 2, 3, 4{"employees":["John", "Anna",[][[1\n\n]{foo: [}{"text": "The quick brown fox won\'t jump"}{"value_1": "value_2": "data"}{"value_1": true, COMMENT "value_2": "data"}- { "test_key": ["test_value", "test_value2"] }{ "content": "[LINK]("https://google.com")" }{ "content": "[LINK](" }{ "content": "[LINK](", "key": true }{"key":"",}(back to top)
To add the JSON Repair to your Go project, use the following command:
go get github.com/RealAlexandreAI/json-repairpackage main
import (
"github.com/RealAlexandreAI/json-repair"
)
func main() {
// broken JSON string from LLM
in := "```json {'employees':['John', 'Anna', ```"
jsonrepair.RepairJSON(in)
// output: {"employees":["John","Anna"]}
}Additionally, there is
MustRepairJSONfor scenarios that are not suitable for error handling, such as pipes and trusted environments
For more examples, please refer to the Test Cases Or Online Playground
(back to top)
RepairJSON and MustRepairJSON keep their existing compact-string behavior.
Headless callers that need the parsed value or advanced repair behavior can use
the library options API:
input := `{"key": }`
result, err := jsonrepair.RepairWithOptions(input, jsonrepair.RepairOptions{
Logging: true,
})
result.Value // parsed Go value
result.JSON // compact repaired JSON
result.Log // repair actions when Logging is trueThe zero value of RepairOptions is the normal repair path. It also supports
Strict, StreamStable, SkipJSONValidation, Logging, and schema-guided
repair. RepairValue is the short form when only the parsed Go value is
needed. RepairReader and RepairFile cover headless stream and file input.
Schema-guided repair accepts JSON Schema objects or boolean schemas. The
standard and salvage modes support object, array, scalar, required,
default, enum, const, items, additionalItems, patternProperties,
additionalProperties, anyOf, oneOf, allOf, local $ref, common string
and numeric constraints, and nested JSON-string containers. Salvage mode can
drop invalid array items and recover schema-matching top-level fragments.
The Go implementation keeps Unicode output by default. Python-only Pydantic
models and Python-specific json.dumps keyword arguments are not part of the
Go API. The existing CLI flags and output behavior remain unchanged.
(back to top)
brew install realalexandreai/tap-jsonrepair/jsonrepair
# from raw string
jsonrepair -i "{'employees':['John', 'Anna', "
# output: {"employees":["John", "Anna", "Peter"]}
# from file
jsonrepair -f .jsonYou can also download binary from Release, please refer to the Releases.
See the open issues for a full list of proposed features (and known issues).
(back to top)
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)(back to top)
Distributed under the GPLv3 License. See LICENSE for more information.
(back to top)
RealAlexandreAI - @RealAlexandreAI
Project Link: https://github.com/RealAlexandreAI/json-repair
(back to top)
No open issues yet, or sync has not completed.