Guide 5 min read

How to format JSON safely at work

Pasting a production API response into a random “JSON beautifier” is the quiet data-leak nobody writes a policy about. Here is what is actually in that payload — and how to fix the parse errors.

What you are really pasting

An API response is rarely just a list of product names. It usually carries access tokens in test headers, internal hostnames, customer email addresses, order identifiers, feature-flag names that reveal unreleased work, and sometimes full personal records. A formatter site is a server that receives that text; even with a “we delete it after an hour” promise, you have no way to verify it.

This is why many security teams now treat clipboard-to-website tools as they treat USB sticks: fine in principle, banned by default. The easy fix is to use a formatter that never makes a network request — the JSON Formatter here runs the browser’s own JSON.parse, so the payload stays inside the tab.

The four errors that catch everyone

1. Trailing commas

{
  "name": "Ada",
  "role": "engineer",
}          ← this comma is fatal in strict JSON

JavaScript allows it, JSON does not. It is the single most common cause of “Unexpected token }”.

2. Single quotes and unquoted keys

{ name: 'Ada', role: "engineer" }   ← neither key nor value is valid JSON

JSON requires double quotes around every key and every string value. Both styles come from copying a JavaScript object out of a console or a code example.

3. Comments

{
  // TODO: remove before prod
  "debug": true
}

JSON has no comment syntax. Some tools (JSONC) accept it; the standard does not, and neither does JSON.parse.

4. Invisible characters

Copying from Word, Slack or a PDF can bring along smart quotes (“ ”), non-breaking spaces or a byte-order mark. They look identical on screen and break parsing at a position that appears empty. If the error points at whitespace, retype the quotes.

Reading an error message correctly

Modern browsers are helpful but terse. A message like:

Unexpected token '}', ..."items": [1,2,3,]" is not valid JSON

points at the character after the problem. The real culprit is the comma immediately before it. General rule: the mistake is usually a few characters before the reported position.

The MySelfPDF formatter converts the browser’s position into a line and column and prints the source line with a caret, which turns a five-minute hunt into five seconds:

line 12, column 3
    ]
  ^  Expected double-quoted property name

Repair mode, and when not to trust it

“Repair” rewrites the four problems above into strict JSON — trailing commas removed, single quotes converted, unquoted keys quoted, comments stripped. It works on a scanner rather than regex, so a comma inside a string such as "a,b,c" is never touched.

It will not fix everything. Do not expect it to handle:

  • Genuinely malformed structure (an unclosed brace, two values where a key should be)
  • Newline-delimited JSON — wrap it in [ ] and add commas first
  • Numbers beyond 2⁵³, which lose precision the moment they become JavaScript numbers

Three habits worth keeping

  1. Format before you commit. A minified JSON blob in a git diff is unreviewable; a formatted one makes unintended changes obvious.
  2. Sort keys on config files. Alphabetical keys keep diffs stable and merge conflicts rare.
  3. Redact before sharing. Replace tokens, emails and account numbers with placeholders before the payload leaves your machine, even in a screenshot.

Format JSON without uploading it

Line/column errors, tree view, repair, sort and minify — all local.

Open JSON Formatter