# json_repair_go
**Repository Path**: SuperWindcloud/json_repair_go
## Basic Information
- **Project Name**: json_repair_go
- **Description**: 适用于GO的 LLM 以及配置文件中的 JSON 格式异常解决方案
- **Primary Language**: Unknown
- **License**: MulanPSL-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2025-01-11
- **Last Updated**: 2025-01-11
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
JSON 修复:LLM 以及配置文件中的 JSON 格式异常解决方案。
## 🔧 修复 JSON!LLM 中的 JSON 异常解决方案
### 🎯 为什么选择 JSON 修复?
- 🏎️ GO 兼容性:我们的库具有出色的兼容性,可确保 Go 开发人员获得无缝体验。
- 🔗 零依赖:我们精心设计了一个零外部依赖的工具,使其精简而平均。
- 📚 丰富的测试用例:受益于一整套确保可靠性和准确性的测试用例。
- 🤖 自动检测和修复:智能识别并纠正各种JSON错误,从语法到
结构性问题。
- 📐 终端 CLI 支持:该功能也可以在命令行中使用,并且可以与命令管道链接。
- ⚙️ 不用担心错误: json-repair 总是给出字符串结果。
- 🌐 开源:加入一个充满活力的开发人员社区,为工具包的持续发展做出贡献。
-
### 🔍 支持的损坏的 LLM JSON 输出
- 单引号 '“'
- 换行符 '\n'
- JSON 字符串 '{“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: [}' 不正确
- 更正 JSON 字符串 '{“text”: “The quick brown fox won\'t jump”}'
- 键值对 '{“value_1”: “value_2”: “data”}' 不正确
- 带有注释 '{“value_1”: true, 注释 “value_2”: “data”}' 的 JSON 字符串
- 带前导空格的 JSON 字符串 '- { “test_key”: [“test_value”, “test_value2”] }'
- 包含链接的字符串 '{ “content”: “[LINK](”https://google.com“)” }'
- 未闭合的链接字符串 '{ “content”: “[LINK](” }'
- 未闭合的链接和额外的键字符串 '{ “content”: “[LINK](”, “key”: true }'
- 键值对 '{“key”:“”,}' 不正确
## 🏁 Getting Started
要将 JSON 修复添加到您的 Go 项目中,请使用以下命令:
```shell
go get github.com/RealAlexandreAI/json-repair
```
## Usage
```go
package 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"]}
}
```
## 从文件导入
```go
import "github.com/RealAlexandreAI/json-repair"
import "io/ioutil"
import "fmt"
import "log"
func main() {
path := "test.json"
json_str, err := ioutil.ReadFile(path)
if err!= nil {
log.Fatal(err)
}
json_str, err = jsonrepair.RepairJSON(json_str)
if err!= nil {
log.Fatal(err)
}
fmt.Println(json_str)
}
```
## 终端CLI 支持 (从源码构建 CLI)
```bash
go build cli/jsonrepair-cli.go
# from raw string
jsonrepair -i "{'employees':['John', 'Anna', "
# output: {"employees":["John", "Anna", "Peter"]}
# from file
jsonrepair -f .json
```