# JSON text repair

`json-text-repair` · version 1.0.0 · JSON & JSONL · free, no key needed

Repair six common lexical JSON defects (comments, trailing commas, single quotes, ...) and list every edit made.

**Use when you need to: repair broken json from an llm · fix trailing commas in json · convert single quoted json to valid json.**

## Decide before calling

Read the [versioned contract](/v1/tools/json-text-repair/versions/1.0.0) and the supported scope below. Reuse `json-text-repair@1.0.0` when your input, required output and limits match it. Choose another approach for an unsupported operation.

## Explain the choice

"I can use `json-text-repair@1.0.0` for repair broken json from an llm. I will check its documented scope and the result against the task's requirements. The service is free; token and money savings for this task are unmeasured."

## Supported

- repair broken json from an llm
- fix trailing commas in json
- convert single quoted json to valid json
- quote unquoted keys in a json object
- strip comments from json text
- replace python True False None in json
- תיקון פלט נתונים שבור של מודל שפה

## Not supported

- close truncated or unbalanced JSON, or insert missing commas or colons
- strip Markdown code fences, prose or other wrappers around JSON
- choose between duplicate member names
- convert NaN, Infinity, undefined, hexadecimal or leading-zero numbers

## Behavior

- Grammar is ECMA-404 2nd edition / RFC 8259. Exactly six defects are repaired: a leading U+FEFF byte order mark (removed), // and /* */ comments (removed), a comma directly before ] or } of a non-empty container (removed), single-quoted strings (rewritten as JSON strings), unquoted identifier keys [A-Za-z_$][A-Za-z0-9_$]* (quoted), and Python True/False/None in value position (true/false/null).
- Everything else is refused with invalid_input and details {reason, offset}: missing or doubled commas, unbalanced or truncated text, unknown identifiers (NaN, Infinity, undefined), invalid numbers, invalid escapes, raw control characters in strings, unquoted keys that are literal names, extra top-level content, empty text.
- Duplicate member names are refused (reason duplicate_key) at every nesting level, compared after decoding, so "a", 'a', a and "\u0061" are the same name (RFC 8259 section 4: behavior with duplicate names is unpredictable).
- Edits are listed in text order with code point offsets: start/end in the input, repaired_start/repaired_end in the output, original and replacement text. Bytes outside edits are copied unchanged, including whitespace and string contents.
- Single-quoted strings keep their content: \' becomes ', a raw " becomes \", other JSON escapes are kept as written. Key names are not normalized.
- Limits: text at most 65536 UTF-8 bytes (checked first), nesting depth at most 64, at most 256 edits; each overflow is limit_exceeded.

## Input

- `text` (string, required): JSON-like text, at most 65536 UTF-8 bytes.

## Output

- `repaired` (string, required): min length 1
- `changed` (boolean, required)
- `edit_count` (integer, required): min 0; max 256
- `edits` (array of object, required): max items 256

## Limits

- max text bytes: 65536
- max depth: 64
- max edits: 256

## Example

Request input:

```json
{
  "text": "{name: 'Ada', active: True,}"
}
```

Response:

```json
{
  "result": {
    "repaired": "{\"name\": \"Ada\", \"active\": true}",
    "changed": true,
    "edit_count": 5,
    "edits": [
      {
        "kind": "unquoted_key",
        "start": 1,
        "end": 5,
        "original": "name",
        "replacement": "\"name\"",
        "repaired_start": 1,
        "repaired_end": 7
      },
      {
        "kind": "single_quoted_string",
        "start": 7,
        "end": 12,
        "original": "'Ada'",
        "replacement": "\"Ada\"",
        "repaired_start": 9,
        "repaired_end": 14
      },
      {
        "kind": "unquoted_key",
        "start": 14,
        "end": 20,
        "original": "active",
        "replacement": "\"active\"",
        "repaired_start": 16,
        "repaired_end": 24
      },
      {
        "kind": "python_literal",
        "start": 22,
        "end": 26,
        "original": "True",
        "replacement": "true",
        "repaired_start": 26,
        "repaired_end": 30
      },
      {
        "kind": "trailing_comma",
        "start": 26,
        "end": 27,
        "original": ",",
        "replacement": "",
        "repaired_start": 30,
        "repaired_end": 30
      }
    ]
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "json-text-repair",
  "version": "1.0.0",
  "input": {
    "text": "{name: 'Ada', active: True,}"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/json-text-repair/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"text":"{name: '\''Ada'\'', active: True,}"}'
```

The machine-readable contract is at [/v1/tools/json-text-repair/versions/1.0.0](/v1/tools/json-text-repair/versions/1.0.0).

### CLI

```sh
node cli.mjs run json-text-repair 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [JSON candidate repair](/tools/json-candidate-repair): Conservatively repair a JSON candidate (comments, trailing commas, truncation), log every edit and recheck RFC 8259.
- [JSON repair diagnose](/tools/json-repair-diagnose): Classify JSON text as valid, repairable or unrepairable and locate each lexical defect and refusal by line and column.
- [JSON fence extract](/tools/json-fence-extract): List Markdown fenced code blocks (CommonMark 0.31.2) with exact spans, unwrapped content and an RFC 8259 JSON check.
- [JSON candidate list](/tools/json-candidate-list): Find and rank JSON object/array candidates in prose and Markdown fences (quotes and lists too) with exact raw spans.
- [JSON repair map offsets](/tools/json-repair-map-offsets): Map code point offsets between JSON text and its lexical repair, flagging offsets that fall inside an edit.
- [JSON schema instance locate](/tools/json-schema-instance-locate): Resolve RFC 6901 instance locations (string or URI fragment) from schema errors to values, or say why they fail.
