# JSON Formatter & Validator

- **URL:** https://codeasystem.com/calculators/developer/json-formatter-validator/
- **Category:** developer
- **Description:** Validate JSON and reformat it with pretty indentation or minify it to a single compact line, with precise parse-error positions.
- **Primary output:** Formatted JSON (2-space indent): {
  "a": 1,
  "b": [
    2,
    3
  ]
}

## Inputs
- JSON (name: `json`, type: textarea, example: {"paste":"your JSON here"})
- Output mode (name: `mode`, type: select)
- Indent width (name: `indent`, type: select)

## Outputs
- Formatted JSON (2-space indent): {
  "a": 1,
  "b": [
    2,
    3
  ]
}
- Root type: object
- Original length: 17 characters
- Formatted length: 39 characters
- Valid JSON: yes; parsed successfully

## Formula / methodology
```
output = JSON.parse(input) then JSON.stringify(parsed, null, indent | undefined)
```

The input is parsed with the same strict JSON grammar browsers use. Valid input is re-serialized either with your chosen indentation or with all insignificant whitespace stripped for minified output.

On failure, the engine’s own syntax-error message is surfaced verbatim, including the character position where parsing stopped; usually pointing at the offending comma, quote or brace.

## Assumptions & limitations
- Duplicate object keys are legal but only the last value survives parsing.
- Trailing commas, comments and single quotes are invalid JSON and will be rejected.
- Very large inputs may be slow; parsing is linear in input size.

## How to use
1. **Paste your JSON**; Raw API responses, config snippets, anything syntactically JSON.
2. **Choose pretty or minify**; Pretty for reading and diffs; minify for embedding in payloads.
3. **Fix errors by position**; If validation fails, jump to the character position named in the error message.

## Example
The compact object {"a":1,"b":[2,3]} pretty-printed at two-space indent becomes a readable five-line document.

Result for these inputs:

```
Formatted JSON (2-space indent): {
  "a": 1,
  "b": [
    2,
    3
  ]
}
```

## About this calculator
### Formatting never changes data

Whitespace is not data in JSON. Pretty-printing and minifying both round-trip losslessly; the parsed value is identical before and after, which makes formatting safe on production payloads.

### Reading V8 error positions

When validation fails, the message quotes the offset where the parser gave up. For a stray trailing comma that position lands just past it, so scanning backwards one or two characters almost always finds the culprit.

### What JSON rejects but JS allows

- Comments; none exist in JSON despite looking like JavaScript
- Single-quoted strings; double quotes only
- Trailing commas; hard error
- NaN / Infinity / undefined; not representable

## FAQs
### Is my JSON sent anywhere?

No. Parsing and formatting happen entirely in your browser via JavaScript’s built-in JSON engine; nothing leaves the page.

### Why does my config file fail here but work in my build tool?

Many tools accept JSONC (comments) or trailing commas. This validator enforces strict RFC 8259 JSON; strip comments and trailing commas first.

### Does pretty printing sort keys?

No. Object key order from JSON.parse is preserved as-is; sorting would change semantics for some consumers.

### Can I format huge files?

Megabyte-scale documents work fine; multi-hundred-megabyte dumps may hit browser memory limits.

## Related calculators
- [Base64 Encoder & Decoder](https://codeasystem.com/calculators/developer/base64-encoder-decoder/)
- [URL Encoder & Decoder](https://codeasystem.com/calculators/developer/url-encoder-decoder/)
- [JWT Decoder](https://codeasystem.com/calculators/developer/jwt-decoder/)

---
Last updated: 2026-08-23 · Version: 1.0.0 · [HTML version](https://codeasystem.com/calculators/developer/json-formatter-validator/)
