python修复json神器:json-repair包(用于大模型返回json不规范)

文章目录

一、简介

文档:https://pypi.org/project/json-repair/

(文末有其他语言修复链接)

# 安装 pip install json-repair 

1、简介

它可以:

(1)修复JSON中的语法错误
缺少引号、逗号位置错误、有未转义的字符以及键值对不完整。
缺少引号、格式错误的值(true、false、null),以及修复损坏的键值结构。

(2)修复格式错误的JSON数组和对象
不完整或损坏的数组/对象,可通过添加必要的元素(如逗号、括号)或默认值(null、“”)来修复。
该库能够处理包含额外非JSON字符的JSON数据,如注释或位置错误的字符,并在保持有效结构的同时清除这些字符。

(3)缺失JSON值的自动补全
自动用合理的默认值(如空字符串或null)填充JSON字段中的缺失值,以确保数据的有效性。

二、使用

1、三种使用方式

from json_repair import repair_json # 将坏的json字符串修复为好的字符串,如果是单纯的字符串,则返回空串 good_json_string = repair_json(bad_json_string)
import json_repair # 你可以使用这个库来完全替代json.loads(): decoded_object = json_repair.loads(json_string)
import json_repair # True是直接返回对象,而不是json字符串 decoded_object = json_repair.repair_json(json_string, return_objects=True)

(示例)示例

# 用于llm返回json格式,用于修复# pip install json-repairimport json from json_repair import repair_json # 1、 broken_json = "{'name': 'Alice', 'age': 20}" # 有问题的JSON(单引号)# 2、 broken_json = '[1, 2, 3, ]' # 数组末尾多余逗号''' #3、 修复注释" { // 这是一条注释 "id": 123, "desc": "test" /* 这是另一条注释 */ } """ '''#4、 llm按照json格式返回有问题: bad_json_string ='你好,这是我按照json格式返回的数据:{"name": "John", "age": 30, "city": "New York'# 修复json字符串 good_json_string = repair_json(bad_json_string)print(good_json_string)# 解析修复后的json data = json.loads(good_json_string)print(data)

2、开发误区

一些使用该库的用户采用以下模式:
这是浪费资源的行为,因为json_repair会自动验证JSON是否有效。如果你仍想进行验证,可以按照下文的说明在调用中添加skip_json_loads=True。

obj ={}try: obj = json.loads(string)except json.JSONDecodeError as e: obj = json_repair.loads(string)...

3、从文件或文件描述符中读取JSON数据

JSON修复功能也可直接替代json.load():

import json_repair try: file_descriptor =open(fname,'rb')except OSError:...with file_descriptor: decoded_object = json_repair.load(file_descriptor)

请注意,库无法捕获任何与输入/输出相关的异常,这些异常需要由您自行处理。

import json_repair # 更简洁的方式try: decoded_object = json_repair.from_file(json_file)except OSError:...except IOError:...

4、非拉丁字符处理

在处理非拉丁字符(如中文、日文或韩文)时,需要将ensure_ascii=False传递给repair_json(),这样才能在输出结果中保留非拉丁字符。

# 示例: repair_json("{'test_chinese_ascii':'统一码'}")# 将会返回{"test_chinese_ascii":"\u7edf\u4e00\u7801"}# 而不是选择ensure_ascii=False: repair_json("{'test_chinese_ascii':'统一码'}", ensure_ascii=False)# 将会返回{"test_chinese_ascii":"统一码"}

5、提升性能

如果你觉得这个库因为使用了json.loads()而运行太慢,可以通过将skip_json_loads=True传递给repair_json来跳过该步骤。例如:

from json_repair import repair_json good_json_string = repair_json(bad_json_string, skip_json_loads=True)

设置return_objects=True总是更快,因为解析器会直接返回一个对象,无需将该对象序列化为JSON格式。
skip_json_loads 只有在你百分百确定该字符串不是有效 JSON 时才更快。
如果你在转义字符串时遇到问题,可以将其作为原始字符串传递,例如:r"string with escaping\""

6、严格模式

默认情况下,json_repair会尽力“修复”输入数据,即使JSON格式完全无效。

在某些情况下,你需要相反的行为,即让解析器报错而不是尝试修复数据。此时可以将strict=True传递给repair_json、loads、load或from_file来启用该模式:

from json_repair import repair_json repair_json(bad_json_string, strict=True)

CLI在使用json_repair --strict input.json时表现出相同的行为(或通过stdin传输数据)。

在严格模式下,解析器一旦遇到重复键、缺失的分隔符、因多余的逗号导致的空键/值对、多个顶级元素或其他模糊结构等问题,就会抛出ValueError错误。当您只需要友好的错误提示来进行验证时,这种模式非常实用,同时您仍可以在代码的其他部分利用json_repair的强大修复功能。

严格模式仍然遵循skip_json_loads=True规则;将两者结合使用可跳过初始的json.loads检查,同时仍能执行严格的解析规则。

7、使用 json_repair 进行流式修复

有时你在传输数据时,需要修复传入的JSON格式。通常情况下这行不通,但你可以将stream_stable传递给repair_json()或loads(),这样就能实现修复功能了:

stream_output = repair_json(stream_input, stream_stable=True)

8、使用 CLI 中的 json_repair

# 查看帮助 $ json_repair -h usage: json_repair [-h][-i][-o TARGET][--ensure_ascii][--indent INDENT][--skip-json-loads][--schema SCHEMA][--schema-model MODEL][--strict][filename] Repair and parse JSON files. positional arguments: filename The JSON file to repair (if omitted, reads from stdin) options:-h,--help show this help message and exit -i,--inline Replace the file inline instead of returning the output to stdout -o TARGET,--output TARGET If specified, the output will be written to TARGET filename instead of stdout --ensure_ascii Pass ensure_ascii=True to json.dumps()--indent INDENT Number of spaces for indentation (Default 2)--skip-json-loads Skip initial json.loads validation --schema SCHEMA Path to a JSON Schema file that guides repairs --schema-model MODEL Pydantic v2 model in'module:ClassName' form that guides repairs --strict Raise on duplicate keys, missing separators, empty keys/values,and similar structural issues instead of repairing them 

三、Java版本

修复json,其他包都没有这个修复的好!

<dependency><groupId>io.github.du00cs</groupId><artifactId>json-repairj</artifactId><version>0.50.0</version></dependency>

1、使用

importorg.jsonrepairj.JsonRepair;importorg.jsonrepairj.ParseResult;publicclassTest{publicstaticvoidmain(String[] args){String str ="你好,这是我按照json格式返回的数据:{\"name\": \"约翰\", \"age\": 30, \"city\": \"New York";// 修复json {"name":"约翰","age":30,"city":"New York"}String result =JsonRepair.repairJson(str);System.out.println(result);// or more control (result = jackson node + warnings)ParseResult result2 =JsonRepair.parseJson(str,true,false);System.out.println(result2.getJson().toString());}}

Read more

QtCreator配置AI辅助编程插件github copilot保姆级教程

QtCreator配置AI辅助编程插件github copilot保姆级教程

文章目录 * 概要 * 配置流程 概要 Free版‌免费使用,每月限额 2000 次代码补全 + 50 次聊天交互‌集成于 VS Code,支持跨文件编辑、终端协助及自定义指令‌ ‌ Pro版‌‌个人用户‌:10 美元/月 或 100 美元/年‌ ‌特殊群体‌:学生/教师/热门开源维护者可免费使用 Pro 版‌ ‌ Business版‌19 美元/月/用户,按月计费‌面向组织或企业中的团队订阅‌ ‌ Enterprise版‌39 美元/月/用户,按月计费‌企业可按需为不同组织分配 Business 或 Enterprise 订阅‌ 官方地址

By Ne0inhk

Alpamayo-R1-10B基础教程:从Load Model到轨迹可视化详解

Alpamayo-R1-10B基础教程:从Load Model到轨迹可视化详解 1. 项目概述 Alpamayo-R1-10B是NVIDIA推出的自动驾驶专用开源视觉-语言-动作(VLA)模型,基于100亿参数架构设计。这个模型通过整合多摄像头视觉输入和自然语言指令,能够生成精确的车辆行驶轨迹预测,并提供可解释的因果推理过程。 1.1 核心组件 * 视觉编码器:处理前视、左侧、右侧摄像头输入 * 语言理解模块:解析自然语言驾驶指令 * 轨迹预测器:生成64个时间步的轨迹坐标 * 因果推理引擎:提供决策过程的透明解释 2. 环境准备 2.1 硬件要求 组件最低要求推荐配置GPURTX 3090 (24GB)RTX 4090 (24GB)内存16GB32GB存储30GB可用空间SSD存储 2.2 软件依赖 确保已安装以下基础环境: # 检查NVIDIA驱动 nvidia-smi # 验证CUDA版本 nvcc --version # 检查Python环境 python --version 3.

By Ne0inhk

Stable Diffusion WebUI本地部署全步骤(含CUDA,cuDNN,Pytorch GPU版安装过程)(Win 11 + RTX5060)

部署SD WebUI前,先安装CUDA+cuDNN+Pytorch 电脑配置: 系统:windows 11 显卡:NVIDIA GeForce RTX 5060 Laptop GPU 内存:24G 下载版本: CUDA:13.0 cuDNN:9.13.1 Pytorch:12.9 第一步:安装CUDA 步骤一:查看CUDA version win+R输入cmd,在命令提示符窗口中输入nvidia-smi,查看CUDA Version 我的CUDA version 为13.0,所以我下载的版本为13.0的(也可以向下安装低版本的,我建议下载最新的版本)。 CUDA下载网址:https://developer.

By Ne0inhk

VS Code 中 Git 的使用:从零到一保姆级菜鸟教程

VS Code 中 Git 的使用:从零到一保姆级菜鸟教程 前言 在现代软件开发中,版本控制是必不可少的技能。VS Code 作为目前最流行的代码编辑器,其内置的 Git 可视化工具让代码管理变得极其直观和简单。 本文将带你从零开始,跑通“下载安装 -> 环境配置 -> GitHub 关联 -> 提交推送 -> 冲突解决”的全流程。告别繁琐的命令行,用可视化的方式优雅地管理代码! 1. 软件下载与基础配置 1.1 下载地址 * VS Code 官方下载:https://code.visualstudio.com/Download * Git 官方下载 (Windows

By Ne0inhk