JSON Repair Kit
A utility to repair broken or "loose" JSON files (like those with trailing commas, single quotes, or unquoted keys) by parsing them as JavaScript objects and re-serializing as valid JSON.
Usage
CODEBLOCK0
Supported Repairs
- - Trailing Commas:
{"a": 1,} -> INLINECODE1 - Single Quotes:
{'a': 'b'} -> INLINECODE3 - Unquoted Keys:
{key: "value"} -> INLINECODE5 - Comments: Removes JS-style comments
// (if parser supports it, standard Node eval may strip them if they are line comments outside of strings). - Hex/Octal Numbers:
0xFF -> INLINECODE9
Safety
- - Backup: Always creates a
.bak file before overwriting (unless --no-backup is used, but default is safe). - Validation: Verifies the repaired content is valid JSON before writing.
- Eval Sandbox: Uses
vm.runInNewContext to parse, ensuring no access to global scope or process. It is safer than eval().
JSON 修复工具
一个用于修复损坏或松散JSON文件(如包含尾随逗号、单引号或未加引号的键)的工具,通过将其解析为JavaScript对象并重新序列化为有效的JSON格式。
使用方法
bash
原地修复文件(创建.bak备份)
node skills/json-repair-kit/index.js --file path/to/broken.json
修复并保存到新文件
node skills/json-repair-kit/index.js --file broken.json --out fixed.json
扫描目录并修复所有.json文件(递归)
node skills/json-repair-kit/index.js --dir config/ --recursive
支持的修复类型
- - 尾随逗号:{a: 1,} -> {a: 1}
- 单引号:{a: b} -> {a: b}
- 未加引号的键:{key: value} -> {key: value}
- 注释:移除JS风格的注释//(如果解析器支持,标准Node的eval可能会去除字符串外部的行注释)
- 十六进制/八进制数字:0xFF -> 255
安全性
- - 备份:覆盖前始终创建.bak备份文件(除非使用--no-backup参数,但默认是安全的)
- 验证:在写入前验证修复后的内容是否为有效的JSON
- Eval沙箱:使用vm.runInNewContext进行解析,确保无法访问全局作用域或进程。比eval()更安全。