json-repair
Repair JSON! A Java library for fixing JSON anomalies generated by LLMs.
json-repair : A library for fixing JSON anomalies generated by LLMs.

What is json-repair?
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.
Getting started
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);
Feature
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:
- Adding the missing right parenthesis;
- Adding the missing right square bracket;
- Cleaning up the redundant commas when the value is in the form of an array;
- Filling in the missing values with "null";
- Patch the missing left parentheses;
- Patch the missing outermost parentheses;
- Patch the lack of quotation marks for strings in individual scenarios;
- Provide the number of custom patching attempts.
- Implement the function of extracting JSON from text strings:
- Extract the strings that conform to the JSON format;
- And support the limited repair of the extracted strings.
Benchmark
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-SECONDS
Benchmark (anomalyJSON) Mode Cnt Score Error Units
BenchmarkTests.testSimpleRepairStrategy {"f":"v", "f2":"v2" avgt 5 114767.597 ± 246035.480 ns/op
BenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1 avgt 5 42658.921 ± 66863.970 ns/op
BenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1,2], "o1":{"f1":"v1"}, avgt 5 410883.872 ± 703643.753 ns/op
BenchmarkTests.testSimpleRepairStrategy "f":"v", "a":[1,2], "o1":{"f1":"v1"} avgt 5 180096.819 ± 535679.217 ns/op
BenchmarkTests.testSimpleRepairStrategy f:v avgt 5 252954.401 ± 908076.158 ns/op
--AverageTime --MILLISECONDS --Warmup-5-1-SECONDS
Benchmark (anomalyJSON) Mode Cnt Score Error Units
BenchmarkTests.testSimpleRepairStrategy {"f":"v", "f2":"v2" avgt 5 0.179 ± 0.256 ms/op
BenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1 avgt 5 0.215 ± 0.373 ms/op
BenchmarkTests.testSimpleRepairStrategy {"f":"v", "a":[1,2], "o1":{"f1":"v1"}, avgt 5 0.070 ± 0.152 ms/op
BenchmarkTests.testSimpleRepairStrategy "f":"v", "a":[1,2], "o1":{"f1":"v1"} avgt 5 0.306 ± 0.121 ms/op
BenchmarkTests.testSimpleRepairStrategy f:v avgt 5 0.125 ± 0.243 ms/op
Coverage
You can check out report to learn about the details of the project's test coverage.
Roadmap
- Make more accurate corrections to abnormal JSON by providing reference JSON styles.
Development Guid
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 install
License
json-repair is licensed under the Apache-2.0 license.
Yorumlar (0)
Yorum birakmak icin giris yap.
Yorum birakSonuc bulunamadi