# JSON5 / JSONC ⇄ JSON Converter

Convert JSON5 or JSONC to strict JSON — strip // comments and trailing commas, quote keys — or turn plain JSON back into JSON5. Private, in-browser, free.

## Run it

- **CLI:** `gizza tool json5-convert "{
  // the dev server port
  port: 8080,
  hosts: ['a', 'b',],
}"`
- **Web:** https://gizza.ai/tools/json5-convert/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/json5-convert/tool.json

## Inputs

- `text` — JSON5 / JSONC / JSON _(field)_
- `direction` — Direction _(field)_
- `indent` — Indentation _(field)_
- `sort_keys` — Sort keys alphabetically _(field)_
- `nonfinite` — NaN / Infinity in strict JSON _(field)_
- `quote_style` — JSON5 quote style _(field)_
- `unquote_keys` — JSON5: leave identifier keys unquoted _(field)_
- `trailing_commas` — JSON5: add trailing commas _(field)_

## Output

- Converted output (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `text` — JSON5 / JSONC / JSON
- `direction` — Direction
- `indent` — Indentation
- `sort_keys` — Sort keys alphabetically
- `nonfinite` — NaN / Infinity in strict JSON
- `quote_style` — JSON5 quote style
- `unquote_keys` — JSON5: leave identifier keys unquoted
- `trailing_commas` — JSON5: add trailing commas

Example: `https://gizza.ai/tools/json5-convert/?text=%7B%0A%20%20%2F%2F%20the%20dev%20server%20port%0A%20%20port%3A%208080%2C%0A%20%20hosts%3A%20%5B%27a%27%2C%20%27b%27%2C%5D%2C%0A%7D&direction=to-json&indent=2&sort_keys=true&nonfinite=null&quote_style=single&unquote_keys=true&trailing_commas=true`

---

## About this tool

**JSON5 / JSONC ⇄ JSON Converter** takes the relaxed JSON people actually write in config files — `tsconfig.json`, VS Code `settings.json`, Babel and ESLint configs, `.jsonc` fixtures — and turns it into strict [RFC 8259](https://www.rfc-editor.org/rfc/rfc8259) JSON that any parser will accept. It also runs the other way, rewriting plain JSON as JSON5 with unquoted keys and single quotes.

The parser reads the full JSON5 grammar, which is a superset of both JSONC and strict JSON:

- **Comments** — `// line` and `/* block */`, anywhere whitespace is allowed.
- **Trailing commas** — after the last element of an array or the last member of an object.
- **Unquoted keys** — bare identifiers such as `port:` instead of `"port":`.
- **Single-quoted strings** — `'a'` as well as `"a"`.
- **Line continuations** — a backslash at end of line joins the next line into the same string.
- **Extra escapes** — `\x41`, `\v`, `\0`, and `\a`-style escapes that stand for the character itself.
- **Loose numbers** — hexadecimal (`0xff`), leading-dot (`.5`), trailing-dot (`5.`), leading-plus (`+1`) and leading-zero (`007`) literals.
- **Non-finite literals** — `NaN`, `Infinity`, `-Infinity`.

Everything JSON5-only is normalized on the way out: numbers become plain decimal literals, keys and strings get double quotes, comments and trailing commas disappear.

### Worked example

JSONC input:

```json5
{
  // the dev server port
  port: 8080,
  /* only these hosts are allowed */
  hosts: ['a', 'b',],
}
```

Strict JSON output (**Direction** = JSON5 / JSONC → strict JSON, **Indentation** = 2 spaces):

```json
{
  "port": 8080,
  "hosts": [
    "a",
    "b"
  ]
}
```

Going the other way, `{"name": "ada", "tags": ["x", "y"]}` with **Direction** = JSON → JSON5 and **JSON5: add trailing commas** ticked becomes:

```json5
{
  name: 'ada',
  tags: [
    'x',
    'y',
  ],
}
```

And loose numeric syntax is normalized — `{ mask: 0xff, ratio: .5, missing: NaN, cap: Infinity }` with **NaN / Infinity in strict JSON** = *Convert to a string* gives:

```json
{
  "mask": 255,
  "ratio": 0.5,
  "missing": "NaN",
  "cap": "Infinity"
}
```

### Options

- **Direction** — *JSON5 / JSONC → strict JSON* (the default), *JSON → JSON5*, or *Auto*, which emits strict JSON when the input used any JSON5-only syntax and JSON5 when the input was already strict.
- **Indentation** — 2 spaces, 4 spaces, a tab, or *Minify* for a single compact line.
- **Sort keys alphabetically** — sorts every object's keys by Unicode code point, at every nesting level, so two configs diff cleanly.
- **NaN / Infinity in strict JSON** — strict JSON cannot spell them, so pick `null` (what `JSON.stringify` does), a string such as `"Infinity"`, or refuse the conversion.
- **JSON5 quote style** — single quotes (JSON5 house style) or double quotes, for JSON5 output.
- **JSON5: leave identifier keys unquoted** — on by default; keys that are plain ASCII identifiers stay bare, anything else stays quoted.
- **JSON5: add trailing commas** — appends a comma after the last element of every non-empty array and object, so adding a line later touches one line in the diff.

### Limits and edge cases

- **Comments carry no data and are always dropped.** JSON has no comment syntax, and this converter emits data only — it does not try to re-attach comments when writing JSON5. If your comments matter, keep the JSON5 file as the source of truth and generate the JSON.
- **Key order is preserved.** Objects keep the order they were written in, so a round trip does not reshuffle your config. Repeated keys collapse last-wins at the first occurrence's position — exactly what a JavaScript object literal does.
- **Number precision is preserved.** Numbers are normalized as *text*, not through a 64-bit float, so `12345678901234567890123` survives intact instead of being rounded.
- **Input caps:** 1 MB of text and 200 levels of nesting. Larger inputs are refused rather than risking an out-of-memory failure in the sandbox.
- Parse errors name the problem and the **line and column** where it was found, e.g. `unterminated string starting at line 2, column 8`.
- Only one top-level value is accepted. Newline-delimited JSON (one object per line) is a different format and is rejected as trailing content.

## FAQ

<details>
<summary>What is the difference between JSON5 and JSONC?</summary>

JSONC ("JSON with Comments") is the dialect Microsoft uses for `tsconfig.json` and VS Code's `settings.json`: strict JSON plus `//` and `/* */` comments and trailing commas. JSON5 is a larger superset that adds unquoted keys, single-quoted strings, line continuations, hexadecimal and leading-dot numbers, and `NaN`/`Infinity`. This converter reads the full JSON5 grammar, so every valid JSONC file is accepted too.

</details>

<details>
<summary>Why did my comments disappear?</summary>

Because strict JSON has no way to express them. The conversion keeps data — objects, arrays, strings, numbers, booleans and nulls — and a comment is not data. That applies in the JSON → JSON5 direction as well: the converter has no comments to restore. Keep the commented file as your source and treat the strict JSON as build output.

</details>

<details>
<summary>What happens to `NaN` and `Infinity`?</summary>

JSON5 allows them, strict JSON does not, so you choose with the **NaN / Infinity in strict JSON** option: `null` (the default, matching `JSON.stringify`), a string like `"NaN"` / `"Infinity"` / `"-Infinity"` if the downstream code re-parses them, or *Refuse the conversion* if a non-finite value means the input is wrong. Writing JSON5 keeps the literals as-is regardless of this setting.

</details>

<details>
<summary>Does converting a big config change my numbers?</summary>

No. Numbers are carried through as text and only re-spelled where JSON5 syntax requires it — `0xff` becomes `255`, `.5` becomes `0.5`, `5.` becomes `5`, `+1` becomes `1` and `007` becomes `7`. Long integers and long decimals are not routed through a floating-point value, so a 23-digit ID stays exactly what you typed.

</details>

<details>
<summary>What does the Auto direction do?</summary>

It looks at what the input actually used. If the text contains any JSON5-only construct — a comment, a trailing comma, a bare key, a single-quoted string, a hex number, `NaN` — it converts to strict JSON. If the input was already strict JSON, it converts to JSON5 instead. It is the one-click "give me the other dialect" mode.

</details>

<details>
<summary>Is my JSON uploaded anywhere?</summary>

No. The converter is a WebAssembly module that runs inside your browser tab. Your config text is parsed and rewritten locally, and you can copy or download the result without it leaving your device — which matters, because config files often hold host names, ports and internal paths.

</details>

## Related tools

- [Absolute value, sign, or negation for a whole column](https://gizza.ai/tools/absolute-value-transformer/): Paste a column of numbers and apply absolute value, sign extraction (-1/0/1), sign flipping, or force-negative to every value at once, with rounding and an audit table.
- [Adjacency Matrix Converter](https://gizza.ai/tools/adjacency-matrix-converter/): Convert a graph between edge list, adjacency matrix, and incidence matrix — directed or undirected, weighted or not. Free, private, runs in your browser.
- [Amazon Order Analyzer](https://gizza.ai/tools/amazon-order-analyzer/): Paste an Amazon order-history CSV export to summarize total spend by month, top items, and category breakdowns. Browser-only, private, with Markdown or JSON output.
- [ARFF Converter](https://gizza.ai/tools/arff-converter/): Convert Weka ARFF datasets to CSV and CSV tables back to ARFF locally — nominal attributes, numeric types, dates, sparse rows, missing values, and type rows.
- [Avro to JSON Converter](https://gizza.ai/tools/avro-to-json/): Decode Apache Avro Object Container Files (.avro / OCF) to JSON, NDJSON, or the embedded schema — no .avsc needed, free and private in your browser.
