JsonRepairNet.Cli 0.1.1

dotnet tool install --global JsonRepairNet.Cli --version 0.1.1
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local JsonRepairNet.Cli --version 0.1.1
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=JsonRepairNet.Cli&version=0.1.1
                    
nuke :add-package JsonRepairNet.Cli --version 0.1.1
                    

JsonRepairNet

dotnet

English | 中文

Origin and license

This repository is a fork of mangiucugna/json_repair (Python package by Stefano Baccianella). The original MIT license and copyright are retained unchanged (see LICENSE). The .NET 8 library and CLI in dotnet/JsonRepairNet are an unofficial community port added in this fork; they are not affiliated with, endorsed by, or maintained by the upstream author. This is a general-purpose developer tool provided as-is; it is not financial advice and should not be relied on to repair data where correctness matters without checking the output.

English

Unofficial port. JsonRepairNet is an unofficial .NET port. It is not affiliated with or endorsed by the upstream project or its author. The repair behavior is based on json_repair by Stefano Baccianella (@mangiucugna), which is released under the MIT license. Credit for the original design and test scenarios belongs to the upstream project. This port covers part of that behavior (see Limits).

A .NET 8 class library and dotnet tool that repair malformed JSON. This port lives beside the Python package and does not replace it.

Status: the NuGet packages JsonRepairNet and JsonRepairNet.Cli (since 0.1.0) are published by jun chen, the owner of this repository. The current version is 0.1.1. You can also build from source (see below).

  • Target framework: net8.0
  • Package id: JsonRepairNet
  • Tool command: json-repair
  • Runtime dependencies: System.Text.Json only (included in the shared framework)

Library

using JsonRepairNet;
using System.Text.Json.Nodes;

string json = JsonRepair.Repair("{name: \"Ada\", ok: True,}");
// {"name":"Ada","ok":true}

if (JsonRepair.TryRepair(text, out string? repaired))
{
    Console.WriteLine(repaired);
}

JsonObject? document = JsonRepair.Repair<JsonObject>(text);
JsonNode? node = JsonRepair.RepairNode(text);
if (JsonRepair.TryRepair(text, out JsonNode? any))
{
    Console.WriteLine(any);
}

string pretty = JsonRepair.Repair(text, JsonRepairOptions.Indented(2));

Repair throws JsonRepairException when the input cannot be repaired. TryRepair returns false for null, blank, or unrecoverable input. JSON null is a successful value: the string form is null, and the node form is a null reference.

Valid JSON is returned unchanged. Set JsonRepairOptions.WriteIndented when you want that text pretty-printed. IndentSize is the number of spaces per level, from 0 through 16.

Build, test, and pack from this repo:

dotnet build dotnet/JsonRepairNet/JsonRepairNet.sln -c Release
dotnet test dotnet/JsonRepairNet/JsonRepairNet.sln -c Release
dotnet pack dotnet/JsonRepairNet/src/JsonRepairNet/JsonRepairNet.csproj -c Release -o /tmp/json-repair-nupkg

To use the packed library from another project, add the output folder as a package source:

dotnet add package JsonRepairNet --version 0.1.1 --source /tmp/json-repair-nupkg

To use the package published on NuGet instead:

dotnet add package JsonRepairNet

CLI

json-repair reads one file or, when the file is omitted, standard input. --output (-o) writes a file. --indent pretty-prints. A following integer sets the indent width; the default width is 2.

json-repair broken.json --output fixed.json --indent 2
cat broken.json | json-repair --indent
json-repair --help

Run it from this repo without installing:

dotnet run --project dotnet/JsonRepairNet/src/JsonRepairNet.Cli -- broken.json --output fixed.json --indent 2

Install the tool locally after packing:

dotnet pack dotnet/JsonRepairNet/src/JsonRepairNet.Cli/JsonRepairNet.Cli.csproj -c Release -o /tmp/json-repair-nupkg
dotnet tool install --global --add-source /tmp/json-repair-nupkg JsonRepairNet.Cli

To install the tool published on NuGet instead, run dotnet tool install --global JsonRepairNet.Cli.

The CLI writes one trailing newline. Exit code 0 means the repair succeeded. Exit code 1 means bad arguments, a missing file, or input that could not be repaired.

Supported repairs

  • Missing quotes around object keys or string values.
  • Missing commas, repeated commas, and trailing commas.
  • Missing closers, a missing opening { before a quoted key, and mismatched ), ], or }.
  • Single quotes and common smart quotes rewritten as JSON double quotes.
  • // line comments, /* */ block comments (including an unclosed block), and # line comments outside strings.
  • Markdown fences (```json … ``` or plain ```) and prose before or after a JSON value.
  • Truncated strings, arrays, and objects. Unclosed containers are closed; a missing value becomes "".
  • True / False / None and other casings of true / false / null, including at the top level.
  • Valid JSON left byte-for-byte as it arrived, including spacing.
  • Underscores inside numbers (82_461_110), a leading dot (.25), and incomplete exponents (1e, 1e+).
  • Values such as 1/3, 10-20, and 1notanumber kept as strings.
  • ... placeholders inside arrays are skipped.
  • Parentheses: (1, 2) becomes an array; (1) stays the scalar 1.
  • When a complete markdown fence is present, each fenced payload is repaired. Several payloads become one JSON array.
  • Several top-level JSON values become one JSON array.
  • Duplicate keys in repaired objects keep the last value.

Limits

  • JSON Schema and Pydantic guidance are not part of this port. strict mode and streaming chunk reads are not part of it either.
  • Invalid input nested deeper than 256 containers fails with JsonRepairException instead of growing the call stack without a bound.
  • A complete ``` fence wins over JSON written outside the fence.
  • Plain prose with no JSON value makes TryRepair return false.
  • The writer emits compact JSON for repairs. It does not try to copy the original spacing of broken input.
  • Some rare string-boundary cases from the Python parser are not reproduced. The scenarios listed above are covered by the test suite.

中文

非官方移植。 JsonRepairNet 是非官方的 .NET 移植,与上游项目及其作者没有隶属关系,也未获得其认可。修复行为参考 Stefano Baccianella(@mangiucugna)的 json_repair,上游以 MIT 许可证发布。原始设计和测试场景的功劳属于上游项目。本移植只覆盖其中一部分行为(见下方“已知限制”)。

这是 json_repair 核心修复能力的 .NET 8 类库和 dotnet 工具,与 Python 包放在同一仓库,互不影响。

状态: NuGet 包 JsonRepairNet 与 JsonRepairNet.Cli(自 0.1.0 起)由本仓库所有者 jun chen 发布,当前版本为 0.1.1。也可以从源码构建(见下文)。

  • 目标框架:net8.0
  • 包名:JsonRepairNet
  • 命令名:json-repair
  • 运行时依赖:只有共享框架里的 System.Text.Json

库

using JsonRepairNet;
using System.Text.Json.Nodes;

string json = JsonRepair.Repair("{name: \"Ada\", ok: True,}");
// {"name":"Ada","ok":true}

if (JsonRepair.TryRepair(text, out string? repaired))
{
    Console.WriteLine(repaired);
}

JsonObject? document = JsonRepair.Repair<JsonObject>(text);
JsonNode? node = JsonRepair.RepairNode(text);
if (JsonRepair.TryRepair(text, out JsonNode? any))
{
    Console.WriteLine(any);
}

string pretty = JsonRepair.Repair(text, JsonRepairOptions.Indented(2));

无法修复时,Repair 抛出 JsonRepairException,TryRepair 返回 false。空串、空白和纯说明文字属于无法修复。JSON 的 null 是成功结果:字符串形式是 null,节点形式是空引用。

合法 JSON 按原文字返回。需要美化时使用 JsonRepairOptions.WriteIndented。IndentSize 是每一级的空格数,范围是 0 到 16。

在本仓库中构建、测试和打包:

dotnet build dotnet/JsonRepairNet/JsonRepairNet.sln -c Release
dotnet test dotnet/JsonRepairNet/JsonRepairNet.sln -c Release
dotnet pack dotnet/JsonRepairNet/src/JsonRepairNet/JsonRepairNet.csproj -c Release -o /tmp/json-repair-nupkg

在其他项目中使用打好的包时,把输出目录作为包源:

dotnet add package JsonRepairNet --version 0.1.1 --source /tmp/json-repair-nupkg

要使用 NuGet 上已发布的包,可以改用:

dotnet add package JsonRepairNet

命令行

json-repair 从文件读取;省略文件时从标准输入读取。--output(-o)写入文件。--indent 美化输出。后面可以跟空格数,默认 2。

json-repair broken.json --output fixed.json --indent 2
cat broken.json | json-repair --indent
json-repair --help

不安装、直接在仓库里运行:

dotnet run --project dotnet/JsonRepairNet/src/JsonRepairNet.Cli -- broken.json --output fixed.json --indent 2

打包后安装:

dotnet pack dotnet/JsonRepairNet/src/JsonRepairNet.Cli/JsonRepairNet.Cli.csproj -c Release -o /tmp/json-repair-nupkg
dotnet tool install --global --add-source /tmp/json-repair-nupkg JsonRepairNet.Cli

要安装 NuGet 上已发布的工具,可以直接运行 dotnet tool install --global JsonRepairNet.Cli。

命令行会在结果末尾加一个换行。退出码 0 表示修复成功。退出码 1 表示参数错误、文件不存在,或输入无法修复。

支持的修复

  • 对象键或字符串值缺少引号。
  • 缺少逗号、重复逗号、尾随逗号。
  • 缺少结束括号、引号键前面缺少 {,以及 )、]、} 不匹配。
  • 单引号和常见弯引号改成 JSON 双引号。
  • 字符串外的 // 行注释、/* */ 块注释(含未闭合块注释)和 # 行注释。
  • Markdown 代码块(```json … ``` 或普通 ```),以及 JSON 前后的说明文字。
  • 截断的字符串、数组和对象。未闭合的容器会被补上;缺失的值变成 ""。
  • True / False / None,以及 true / false / null 的其他大小写,包括出现在顶层时。
  • 合法 JSON 原样保留,包括空格和换行。
  • 数字里的下划线(82_461_110)、前导小数点(.25)和不完整指数(1e、1e+)。
  • 1/3、10-20、1notanumber 这类值保留为字符串。
  • 数组里的 ... 占位会被跳过。
  • 圆括号:(1, 2) 变成数组,(1) 保持标量 1。
  • 存在成对的 ``` 代码块时,只修复代码块里的内容。多段代码块会合成一个 JSON 数组。
  • 多个顶层 JSON 值会合成一个 JSON 数组。
  • 修复对象时,重复键保留最后一次的值。

已知限制

  • 本移植不包含 JSON Schema、Pydantic 引导修复、strict 模式和分块流式读取。
  • 非法输入的容器嵌套超过 256 层时,会抛出 JsonRepairException,避免调用栈无限增长。
  • 成对的 ``` 代码块优先于围栏外的 JSON。
  • 只有说明文字、没有 JSON 值时,TryRepair 返回 false。
  • 修复结果使用紧凑 JSON,不会沿用损坏输入原来的空格。
  • Python 解析器里少数很细的字符串边界情况没有逐条照搬。上面列出的场景由测试覆盖。
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
0.1.1 119 10/29/2026
0.1.0 102 10/26/2026