Skip to content

JSON Formatter guide

Tool guide. Updated .

How the XGM JSON Formatter validates JSON with the browser's strict parser, what common errors mean and how to fix trailing commas, quotes and more.

What valid JSON looks like

JSON (JavaScript Object Notation, RFC 8259) is a text format for structured data built from objects, arrays, strings, numbers, booleans and null. It looks like JavaScript, but it is much stricter. Many inputs that JavaScript accepts, such as unquoted keys or trailing commas, are not valid JSON, and a strict parser rejects them.

JSON value types
TypeExampleRules
Object{"name": "XGM"}Keys must be strings in double quotes
Array[1, 2, 3]Comma-separated, no trailing comma
String"hello\n"Double quotes only; control characters must be escaped
Number42, -1.5, 2e10No leading zeros, no NaN or Infinity, no hex
Booleantrue, falseLowercase
NullnullLowercase
What the formatter does with your inputThe input is parsed strictly; on success it is formatted or minified with the chosen indent and optional key sorting, and on failure the position of the first error is shown.Paste JSONStays in the page; nothing is uploadedStrict parse (RFC 8259)The browser's JSON parser, same as JSON.parseError?Line and column of the first problem, with themessageSuccessPretty-print with 2 spaces, 4 spaces or tabs,or minifyOptional: sort keysObject keys ordered alphabetically at everylevel
The input is parsed strictly; on success it is formatted or minified with the chosen indent and optional key sorting, and on failure the position of the first error is shown.

How to use the JSON Formatter

  1. Open the JSON Formatter and paste your JSON, or press Load example.
  2. Choose the indent (2 spaces, 4 spaces or tab) and whether keys should be sorted.
  3. Press Format (or Ctrl+Enter) to pretty-print, or Minify to remove whitespace.
  4. If the input is invalid, read the error and its line and column, fix it and try again.
  5. Copy the result or export it; nothing is uploaded.

Sorting keys is useful for comparing two JSON documents: once both are formatted with sorted keys, a text diff shows real differences instead of reordered fields. It does not change arrays, whose order is part of the data.

Common errors and fixes

Frequent JSON syntax errors
MistakeInvalidValid
Trailing comma{"a": 1,}{"a": 1}
Single quotes{'a': 'b'}{"a": "b"}
Unquoted key{a: 1}{"a": 1}
Comment{"a": 1 // note}Remove comments
NaN or Infinity{"x": NaN}{"x": null} or a string
Unescaped newline in string"line 1 + newline + line 2""line 1\nline 2"
Missing comma{"a": 1 "b": 2}{"a": 1, "b": 2}
Leading zero{"n": 007}{"n": 7}

The line and column in the error point at where the parser noticed the problem, which is often just after the real mistake. A missing comma is reported at the start of the next value, and an unclosed string can be reported many lines later. Look at the position and the few characters before it.

Configuration files with comments

Formats such as JSONC, JSON5 or the tsconfig.json style allow comments and trailing commas. They are not JSON, so a strict validator reports errors for them. Remove comments or use a tool for that format.

Numbers, precision and large values

JSON itself does not limit number size, but JavaScript represents numbers as 64-bit floating point values. Integers larger than 2^53 − 1 (9007199254740991) lose precision when parsed in a browser. A 19-digit ID can come out of formatting with its last digits changed.

Precision loss with large integers
Input:  {"id": 12345678901234567891}
Parsed: {"id": 12345678901234567000}

APIs that use large IDs, such as some social networks and databases, often send them as strings for this reason. If you are debugging such data, keep a copy of the raw text, and be careful with formatted output of large integers. The same applies to very precise decimal values, which floating point cannot represent exactly.

Privacy when working with JSON

JSON often contains API responses with personal data, tokens or internal identifiers. The XGM formatter processes everything in your browser, and the input is not stored, sent to the server or added to the URL. Browser-only tools are also never recorded in the local recent-checks history.

Even so, avoid pasting live secrets such as API keys or session tokens into any web page when you can use a local tool instead. Replace sensitive values with placeholders before sharing formatted JSON in tickets or chats.

Formatting JSON locally on the command line
# Python
python3 -m json.tool --indent 2 data.json

# jq
jq . data.json
jq -S . data.json   # sorted keys
jq -c . data.json   # minified

JSON frequently needs converting or inspecting in other forms. The CSV to JSON mode turns spreadsheets exports into JSON arrays, the JWT Decoder reads JSON inside tokens, and the Code Formatter handles the other common data format. For API debugging, the HTTP Headers Checker shows the Content-Type a server sends.

A quick way to inspect an API response is to fetch it with curl and paste the body into the formatter. For example, curl -s https://api.example.com/v1/orders/42 returns a single line of minified JSON; formatted, its nesting and field names become easy to read. Remove tokens and personal data from the output before sharing it.

When an API returns an HTML error page instead of JSON, the formatter reports an error at line 1, column 1, because < is not valid JSON. That usually means the request hit a proxy, login page or server error rather than the API itself.

A valid JSON response should be served as application/json. Servers that send JSON as text/html can trigger browser content sniffing issues and make clients guess, which is one reason the X-Content-Type-Options: nosniff header exists.

FAQ

Is my JSON uploaded?

No. Parsing and formatting happen in your browser, and the input is not stored or added to the URL.

Why is my JavaScript object not valid JSON?

JSON requires double-quoted keys and strings and forbids trailing commas, comments, undefined, functions and special numbers such as NaN.

Does the formatter change my data?

Formatting only changes whitespace. Sorting keys changes the order of object keys. Very large integers can lose precision because browsers parse numbers as floating point.

Can it handle large files?

It handles typical API responses and configuration files well. Very large documents of many megabytes can make the page slow, because everything happens in the browser tab.

What does the error position mean?

Line and column where the parser found the problem. The actual mistake is often just before that position, such as a missing comma or quote.

Does JSON allow duplicate keys?

RFC 8259 says names should be unique, and parsers handle duplicates differently. The browser parser keeps the last value, so duplicates silently disappear in formatted output.

Should I use 2 or 4 spaces?

It is a style choice. Two spaces is common in web projects, four in some languages' conventions. Minified JSON is best for transport.

What is the difference between format and minify?

Format adds indentation and line breaks for reading. Minify removes all unnecessary whitespace for the smallest size.

Why does valid-looking JSON fail to parse?

Invisible characters are a common cause: non-breaking spaces or typographic quotes copied from documents or chat apps look like normal spaces and quotes but are not. Retype the quotes around the reported position.

Is JSON with a byte order mark valid?

RFC 8259 says implementations must not add a byte order mark to JSON sent over networks, and parsers may ignore it. Remove it if a strict parser complains about the first character.

Sources