
json-repair is a Java library that provides repair for abnormal JSON generated by LLM (Large Language Model) at the application layer. Based on json-repair, the repair of abnormal JSON can be achieved simply, efficiently and accurately.
To get started with json-repair, first add it as a dependency in your Java project. If you're using Maven, that looks like this:
<dependency> <groupId>io.github.haibiiin</groupId> <artifactId>json-repair</artifactId> <version>0.4.0</version></dependency>If you're using Gradle, that looks like this:
implementation 'io.github.haibiiin:json-repair:0.4.0'
Next, You can instantiate a JSONRepair object then call handle() function to repair JSON string like so.
JSONRepair repair = new JSONRepair();String correctJSON = repair.handle(mistakeJSON);If you need to extract content that conforms to the JSON format from a text string, you need to enable the extraction function through JSONRepairConfig.
JSONRepairConfig config = new JSONRepairConfig();config.enableExtractJSON();JSONRepair repair = new JSONRepair(config);String correctJSON = repair.handle(mistakeJSON);You can learn about all the JSON exceptions that the current version 0.4.0 of json-repair supports for repair by checking the test case dataset or test report.
The functions based on the current version 0.4.0 are as follows:
Implement the basic repair function for JSON strings:
Implement the function of extracting JSON from text strings:
You can conduct performance tests in more scenarios by running BenchmarkTests.
The benchmark based on the current version 0.4.0 as follows:
--AverageTime --NANOSECONDS --Warmup-5-1-SECONDSBenchmark (anomalyJSON) Mode Cnt Score Error UnitsBenchmarkTests.testSimpleRepairStrategy {"f":"v", "f2":"v2" avgt 5 114767.597 ± 246035.480 ns/opBenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1 avgt 5 42658.921 ± 66863.970 ns/opBenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1,2], "o1":{"f1":"v1"}, avgt 5 410883.872 ± 703643.753 ns/opBenchmarkTests.testSimpleRepairStrategy "f":"v", "a":[1,2], "o1":{"f1":"v1"} avgt 5 180096.819 ± 535679.217 ns/opBenchmarkTests.testSimpleRepairStrategy f:v avgt 5 252954.401 ± 908076.158 ns/op--AverageTime --MILLISECONDS --Warmup-5-1-SECONDSBenchmark (anomalyJSON) Mode Cnt Score Error UnitsBenchmarkTests.testSimpleRepairStrategy {"f":"v", "f2":"v2" avgt 5 0.179 ± 0.256 ms/opBenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1 avgt 5 0.215 ± 0.373 ms/opBenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1,2], "o1":{"f1":"v1"}, avgt 5 0.070 ± 0.152 ms/opBenchmarkTests.testSimpleRepairStrategy "f":"v", "a":[1,2], "o1":{"f1":"v1"} avgt 5 0.306 ± 0.121 ms/opBenchmarkTests.testSimpleRepairStrategy f:v avgt 5 0.125 ± 0.243 ms/op
You can check out report to learn about the details of the project's test coverage.
Make more accurate corrections to abnormal JSON by providing reference JSON styles.
After cloning the code to your local machine, navigate to the project's root directory. Build and install all modules, it’ll install modules into Maven local repository cache, and also generate Java class files of parser from ANTLR grammar .g4 files to prevent from compile error of parser on IDE.
mvnw installjson-repair is licensed under the Apache-2.0 license.