JSON 修復工具
自動修復並還原損壞或格式錯誤的 JSON——安全、快速且免費。
此工具透過自動錯誤修正來修復損壞或無效的 JSON。它專注於解決常見語法問題,例如匯出資料、日誌或手動編輯檔案中缺少引號、多餘逗號或括號不匹配等問題,並輸出有效且可解析的 JSON。
修復您下面的 JSON
JSON 修復運作原理
該工具會掃描您的輸入,檢查常見的 JSON 語法問題,並針對性地進行修正,盡可能產生有效且符合標準的 JSON。
- 解析輸入並定位常見語法錯誤,包括非法字元、錯置的括號及不正確的字串引號。
- 修正常見錯誤,如添加遺漏的引號、移除多餘的逗號,或更正括號。
- 輸出已修復的 JSON。在大多數情況下,結果即可立即解析或使用。
- 如果結構無法自動修復,系統會顯示明確錯誤訊息,方便您調整輸入內容。
常見的 JSON 錯誤及其修復方法
以下是此工具能修復的常見 JSON 錯誤,並附有每種錯誤的範例:
未加引號的鍵
鍵必須使用雙引號。
Before:
{ name: "鮑勃" }
After:
{ "name": "鮑勃" }
修正:為所有未加引號的物件鍵加上雙引號。
使用單引號
JSON 僅接受雙引號作為字串的界定符。
Before:
{ 'role': '管理員' }
After:
{ "role": "管理員" }
修正:將所有字串中的單引號替換為雙引號。
後置逗號
最後一項後不得加逗號。
Before:
{ "a": 1, "b": 2, }
After:
{ "a": 1, "b": 2 }
修正:移除任何結尾的逗號。
括號不匹配或未閉合
所有括號與大括號必須正確閉合。
Before:
[1, 2, 3
After:
[1, 2, 3]
修正:新增遺漏的結尾括號或大括號。
缺少或多餘的逗號
陣列和物件項目必須以單一逗號分隔。
Before:
{ "a": 1 "b": 2 }
After:
{ "a": 1, "b": 2 }
修正:插入缺失的逗號,或移除重複項目。
存在註解行
標準 JSON 不允許註解(例如 // 或 /* ... */)。
Before:
{ "id": 1, // 使用者 ID "active": true }
After:
{ "id": 1, "active": true }
修正:從輸入中移除註解行。
無效的數字格式
僅接受純十進位數字(不含非數值 NaN、無限大 Infinity 或十六進位數)。
Before:
{ "val": 非數值 }
After:
{ "val": null }
修正:將無效的數字替換為空值。
未轉義的控制字元
字串中的換行符等字元必須進行跳脫處理。
Before:
{ "msg": "第一行 第二行" }
After:
{ "msg": "第一行\n第二行" }
修正:正確轉義控制字元。
物件中的重複鍵
同一物件中重複的鍵會引起歧義。
Before:
{ "name": "A", "name": "B" }
After:
{ "name": "B" }
修正:對於任何重複的鍵,僅保留最後一個值。
錯誤格式的陣列
陣列中的元素必須使用方括號並以逗號分隔。
Before:
[ 1 2 3 ]
After:
[ 1、2、3 ]
修正:在陣列元素之間新增遺漏的逗號。
輸入為空或僅包含空白字元
JSON 內容不可為空。
Before:
After:
空
修正:輸入為空時返回 null。
範例:從損壞的 JSON 修復到有效格式
格式錯誤的 JSON 輸入
{ user: 'alice', id: 42, roles: [管理員, '編輯者',] // 多餘的逗號 active: true }已修復的 JSON 輸出
{ "user": "alice", "id": 42, "roles": [ "管理員", "編輯者" ], "active": true }
如何使用此 JSON 修復工具
- 請將失效的 JSON 貼上或輸入到以下編輯器中。
- 點擊「修復」自動檢測並修正常見語法錯誤。
- 檢查修復後的輸出並複製修正好的 JSON。
在使用修復後的 JSON 處理重要資料前,請務必仔細檢查—複雜或嚴重損壞的輸入可能需要手動調整。
JSON 修復範例程式碼
了解如何在多種程式語言中修復 JSON。
JavaScript (Node.js) with jsonrepair
Install: npm install jsonrepair
const { jsonrepair } = require('jsonrepair');
const broken = '{ name: "Bob", age: 42, }';
const fixed = jsonrepair(broken);
console.log(fixed); // Now valid JSON!
JavaScript (Node.js) simple fix (not for production)
Install: Standard library
let broken = "{ name: 'Bob', age: 42 }";
broken = broken.replace(/(['"])?:([\s]*)([^\s,\{\}\[\]":']+)/g, '"$1$3":');
try {
let obj = JSON.parse(broken);
console.log(obj);
} catch (e) {
console.error('Still broken:', e.message);
}
Python with dirtyjson
Install: pip install dirtyjson
import dirtyjson
broken = "{ name: 'Bob', age: 42 }"
obj = dirtyjson.loads(broken)
print(obj)
Python with demjson3
Install: pip install demjson3
import demjson3
broken = "{ name: 'Bob', age: 42 }"
obj = demjson3.decode(broken)
print(obj)
Go (using otto for JS-like object parsing)
Install: go get github.com/robertkrimen/otto
package main
import (
"fmt"
"github.com/robertkrimen/otto"
)
func main() {
vm := otto.New()
broken := "{ name: 'Bob', age: 42 }"
value, err := vm.Run("(" + broken + ")")
if err != nil {
fmt.Println("Cannot repair:", err)
} else {
obj, _ := value.Export()
fmt.Println(obj)
}
}
Java with org.json (manual fix for single quotes)
Install: org.json:json
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String broken = "{'name': 'Bob', 'age': 42}".replace(''', '"');
JSONObject obj = new JSONObject(broken);
System.out.println(obj);
}
}
C# with Newtonsoft.Json (try-catch, manual fix)
Install: Newtonsoft.Json
using System;
using Newtonsoft.Json;
class Program {
static void Main() {
var broken = "{ name: 'Bob', age: 42 }".Replace("'", "\"");
try {
var obj = JsonConvert.DeserializeObject(broken);
Console.WriteLine(obj);
} catch (Exception ex) {
Console.WriteLine("Broken JSON: " + ex.Message);
}
}
}
PHP (manual fix for single quotes/unquoted keys)
Install: Standard library
<?php
$broken = "{ name: 'Bob', age: 42 }";
$fixed = preg_replace("/'([^"]*)'/", '"$1"', $broken);
$fixed = preg_replace('/([a-zA-Z0-9_]+):/', '"$1":', $fixed);
$obj = json_decode($fixed);
var_dump($obj);
Ruby with json-repair (via gem or shell call)
Install: gem install json-repair or use npx jsonrepair
# Using system call to npx jsonrepair
require 'open3'
broken = "{ name: 'Bob', age: 42 }"
fixed, _ = Open3.capture2("echo #{broken.inspect} | npx jsonrepair")
puts fixed
Bash (npx jsonrepair CLI)
Install: npm install -g jsonrepair
echo "{ name: 'Bob', age: 42 }" | npx jsonrepair
Rust (suggest manual pre-processing)
Install: Standard libraries
// Rust does not have a json repair crate yet. Pre-process string with regex to fix simple cases, then use serde_json.
Kotlin (manual fix, like Java)
Install: org.json:json
import org.json.JSONObject
fun main() {
var broken = "{ name: 'Bob', age: 42 }".replace("'", "\"")
broken = Regex("([a-zA-Z0-9_]+):").replace(broken, ""$1":")
val obj = JSONObject(broken)
println(obj)
}
TypeScript (Node.js) with jsonrepair
Install: npm install jsonrepair
import { jsonrepair } from 'jsonrepair';
const broken = '{ name: "Bob", age: 42 }';
const fixed = jsonrepair(broken);
console.log(fixed);
Dart (manual string fixes)
Install: Standard library
var broken = "{ name: 'Bob', age: 42 }";
var fixed = broken.replaceAll("'", '"').replaceAllMapped(
RegExp(r'([a-zA-Z0-9_]+):'),
(m) => '"${m[ 1 ]}":',
);
print(fixed);
Elixir (manual fix with Regex)
Install: Standard library
broken = "{ name: 'Bob', age: 42 }"
fixed = Regex.replace(~r/'/, broken, """)
fixed = Regex.replace(~r/(\w+):/, fixed, ""\\1":")
IO.puts(fixed)