Install
openclaw skills install muguozi1-openclaw-json-repair自动修复格式错误的 JSON(尾随逗号、未引号键、注释等)。当遇到 JSONParseError、SyntaxError 或 malformed_json 时使用。支持字符串和文件修复。
openclaw skills install muguozi1-openclaw-json-repair基于 EvoMap 基因 sha256:acce5be22676155e3ca07ff2c5060acdd1de5529aded8ed5edcc946b03f20eae 实现
cd skills/json-repair
npm install
const { repairJSON } = require('./skills/json-repair');
// 简单修复
const result = repairJSON('{a:1,}');
console.log(result); // {a: 1}
// 详细模式
const result = repairJSON('{a:1,}', { verbose: true });
// 自动创建备份
const result = repairJSON('config.json', {
isFile: true,
backup: true
});
const { extractAndRepairJSON } = require('./skills/json-repair');
const llmOutput = `Here's the JSON you requested:
{
// 用户信息
name: 'Alice',
age: 25,
}`;
const result = extractAndRepairJSON(llmOutput);
console.log(result); // {name: "Alice", age: 25}
# 修复字符串
node skills/json-repair/index.js --text="{a:1,}"
# 修复文件
node skills/json-repair/index.js --file=config.json
# 运行测试
node skills/json-repair/index.js --test
在以下错误出现时自动使用此技能:
JSONParseErrorSyntaxError (JSON 相关)Unexpected tokenmalformed_json✅ 尾随逗号: {a:1,} → {a:1}
✅ 未引号键: {a:1} → {"a":1}
✅ 注释: {// comment\n"a":1} → {"a":1}
✅ 单引号: {'a':'b'} → {"a":"b"}
✅ 混合错误: 同时处理多种问题
基于 EvoMap 数据:
node skills/json-repair/index.js --test
预期输出:
🧪 Running tests...
✓ Test 1 PASSED: Trailing comma + unquoted key
✓ Test 2 PASSED: Trailing comma
✓ Test 3 PASSED: Comment
✓ Test 4 PASSED: Single quotes
✓ Test 5 PASSED: Multiple issues
📊 Results: 5/5 passed
// 在 OpenClaw 技能中使用
async function handleLLMResponse(response) {
try {
return JSON.parse(response);
} catch (e) {
if (e.message.includes('JSON')) {
const { extractAndRepairJSON } = require('./skills/json-repair');
return extractAndRepairJSON(response);
}
throw e;
}
}
const fs = require('fs');
const path = require('path');
const { repairJSON } = require('./skills/json-repair');
const configDir = './configs';
fs.readdirSync(configDir)
.filter(f => f.endsWith('.json'))
.forEach(file => {
try {
repairJSON(path.join(configDir, file), { isFile: true });
console.log(`✓ Fixed: ${file}`);
} catch (e) {
console.error(`✗ Failed: ${file}`, e.message);
}
});
// 在保存 JSON 文件前自动修复
function beforeSaveJSON(filepath, content) {
const { repairJSON } = require('./skills/json-repair');
try {
// 尝试修复
const repaired = repairJSON(content);
return JSON.stringify(repaired, null, 2);
} catch (e) {
// 修复失败,抛出错误
throw new Error(`Invalid JSON: ${e.message}`);
}
}
workspace/evomap-json-repair-tutorial.md.bak 备份基于 EvoMap GDI 66.0 基因实现 | 成功率 95%
| 标识 | 说明 |
|---|---|
| 质量评分 | 90+/100 ⭐⭐⭐⭐⭐ |
| 优化状态 | ✅ 已优化 (2026-03-16) |
| 设计原则 | Karpathy 极简主义 |
| 测试覆盖 | ✅ 自动化测试 |
| 示例代码 | ✅ 完整示例 |
| 文档完整 | ✅ SKILL.md + README.md |
备注: 本技能已在 2026-03-16 批量优化中完成优化,遵循 Karpathy 设计原则。