Back to blog

How to Format and Validate JSON

To format JSON, paste it into a formatter that adds indentation and line breaks so the structure is easy to read; to validate it, run it through a parser that checks the syntax and points to the exact spot where it breaks. Formatting (also called beautifying or pretty-printing) and validating usually happen together: a good tool re-indents valid JSON and, when the input is broken, tells you precisely what is wrong. Below is what each action actually does, when to format versus minify, and how to read and fix the errors a parser throws.

Format, minify, validate: three different jobs

These three words get used interchangeably, but they do distinct things to your data.

  • Formatting (beautifying) adds indentation and line breaks so nested objects and arrays line up visually. It does not change the data — only the whitespace — making it far easier to scan a config file or an API response by eye.
  • Minifying does the opposite: it strips every non-essential space, tab, and newline so the payload is as small as possible. The data is identical, just packed onto effectively one line.
  • Validating checks whether the text is legal JSON at all. A validator parses the input and either confirms it is well-formed or reports the first syntax error it hits.

In practice you format while you are reading and debugging, and you minify right before sending data over the wire or storing it. Validation underpins both — you cannot reliably format or minify text that is not valid JSON in the first place.

When to format vs minify

Reach for formatting whenever a human needs to look at the data:

  • Inspecting an API response while debugging.
  • Reviewing or editing a package.json, tsconfig.json, or any config file.
  • Diffing two JSON files in version control, where consistent indentation keeps the diff small and meaningful.
  • Pasting a dense one-line blob from a log and wanting to actually understand its shape.

Reach for minifying when the bytes matter and no human is reading:

  • Shipping JSON in an HTTP response or request body, where smaller payloads mean faster transfers.
  • Embedding configuration in an environment variable or a URL.
  • Storing many records where whitespace adds up across thousands of rows.

A useful habit: keep your source files formatted for readability, and let your build or server minify on the way out. You get the best of both — readable in the repo, compact on the wire.

The most common JSON errors (and how to fix them)

JSON is deliberately strict, which is what makes it portable across every language. That same strictness trips people up, especially anyone used to writing JavaScript object literals. These are the errors that come up again and again:

  • Trailing commas. A comma after the last item in an object or array — {"a": 1,} — is invalid. Remove it. This is the single most common mistake, because most programming languages allow it.
  • Single quotes instead of double quotes. JSON requires double quotes for both keys and string values. {'name': 'Ada'} must become {"name": "Ada"}.
  • Unquoted keys. Keys are always quoted strings. {name: "Ada"} is a JavaScript object, not JSON; it must be {"name": "Ada"}.
  • Missing commas. Every item except the last needs a comma separating it from the next. Two values in a row with nothing between them will fail to parse.
  • Comments. JSON has no comment syntax. Lines starting with // or wrapped in /* */ are not allowed and must be stripped out before the text will validate.

A few more to watch for: numbers cannot have leading zeros or a trailing decimal point, strings must escape special characters like \n and \", and the only allowed literals besides numbers and strings are true, false, and null (lowercase, unquoted).

How to read a parse error

When validation fails, the parser usually reports a position — a line and column, or a character offset. Read that location literally: the error is reported at the first character the parser could not make sense of, which is often just after the real mistake. For example, a missing comma is frequently flagged at the start of the next key, not at the gap where the comma should have been. Jump to the reported spot, then look at the token immediately before it. Once you fix the first error, re-validate, because a parser stops at the first problem and there may be more behind it.

Do it privately in your browser

A lot of online JSON tools send whatever you paste to a remote server for processing. With JSON that is often the worst possible data to upload, because it is exactly where API keys, access tokens, internal hostnames, customer records, and other secrets tend to live.

Andev’s JSON Formatter avoids that entirely. It uses the browser’s own native JSON.parse and JSON.stringify, which means:

  • Your data never uploads. Everything runs locally on your device; nothing is sent anywhere.
  • It surfaces the exact parse error. When the input is invalid, you see the specific message and location so you can go straight to the problem.
  • You choose the indent size. Format with two spaces, four spaces, or tabs to match your project’s style — or minify to a single line.
  • It is instant. No upload-and-wait round trip; the result appears as fast as you can paste.

Because it leans on the same JSON engine your applications already use, the validation it reports matches how your code will actually behave when it parses the same string. That makes it safe to use with sensitive configuration and API payloads, and reliable as a quick sanity check before you ship.

For neighboring tasks, the same local, no-upload approach powers Andev’s Base64 encoder and decoder and its URL encoder and decoder — both handy when JSON is being embedded in a data URI, a token, or a query string.

Key takeaways

  • Formatting adds indentation for readability; minifying strips whitespace for smaller payloads; validating checks the syntax. They are three separate jobs.
  • Format for humans, minify for the wire — keep source files readable and let your build compact the output.
  • Most errors are strictness traps: trailing commas, single quotes, unquoted keys, missing commas, and comments are all invalid JSON.
  • Read parse errors at the reported position, then check the token just before it; fix one error and re-validate, since the parser stops at the first.
  • Keep it local. A browser-native formatter never uploads your data, which matters most for the secrets that live inside config and API JSON.

Need to clean up or check some JSON right now? Open the free, private JSON Formatter — no uploads, no sign-up — or browse the full set of in-browser developer tools.