UtilityNest
Text

How to Find and Fix Invalid JSON (and Format It Nicely)

By the UtilityNest team · · 4 min read

A computer screen showing colorful lines of code
Photo by Chris Ried on Unsplash

You added two lines to your app's settings file, saved it, and the whole thing refused to start. The log says: SyntaxError: Unexpected token } in JSON at position 214. The file is one long line pasted from somewhere, and you are now counting characters with your finger on the screen.

JSON is strict. One extra comma or one single quote and the entire file is rejected. The good news is that almost every broken JSON file comes down to the same handful of mistakes, and a decent validator points straight at the problem.

The short version: paste the JSON into the JSON Formatter. If it is invalid, you get the line, the column and a plain-English reason, plus a Fix automatically button for the common cases. If it is valid, it is beautified with your choice of indent.

Finding the error

  1. Paste the JSON into the JSON Formatter, drop a .json file onto it, or click Open file.
  2. If it is invalid, read the error: line, column, the offending line with a marker under the spot, and the likely cause.
  3. Click Show in editor to jump to it, or Fix automatically when it is offered.
  4. Once valid, choose Beautify or Minify, an indent of 2 spaces, 4 spaces or tabs, and Sort keys if you want them A to Z.

It all runs in your browser, so API responses, tokens and customer data are never uploaded.

A worked example

Here is a settings snippet copied from a JavaScript file:

{ name: 'Shop', debug: True, ports: [8080, 8081,], // dev only }

It has five problems: unquoted keys, single quotes, Python-style True, a trailing comma and a comment. The validator stops at the first one, the unquoted key at line 1, column 3 ("keys must be in double quotes"). Fix automatically repairs all of them at once:

{ "name": "Shop", "debug": true, "ports": [8080, 8081] }

"Unexpected token at position 214", and a file you have to read character by character.

Line 1, column 3, the reason in plain words, and a one-click fix you can review.

The six mistakes behind most broken JSON

MistakeWrongRight
Trailing comma[1, 2, 3,][1, 2, 3]
Single quotes{'a': 'b'}{"a": "b"}
Unquoted keys{a: 1}{"a": 1}
Comments// noteNot allowed in JSON
Python valuesTrue, Nonetrue, null
Missing comma{"a": 1 "b": 2}{"a": 1, "b": 2}

There is also a sneaky seventh: a real line break inside a string. JSON needs \n instead. The validator calls that out too.

Fix automatically handles comments, trailing commas, unquoted keys, single-quoted strings and Python's True, False and None. Text inside strings is never changed. A missing comma needs a human, because only you know where one value ends and the next begins.

Files like tsconfig.json and VS Code's settings allow comments because they use JSONC, a relaxed variant. If you are copying from one of those into an API request or a strict parser, run it through Fix automatically first to strip the comments.

Beautify, minify and sort

  • Beautify spreads the JSON over lines with indentation, so humans can read it.
  • Minify strips all spaces and line breaks for the smallest possible payload, useful for API requests and config values stored in a single field.
  • Sort keys orders every object's keys alphabetically. Sort two versions of a file and they become far easier to compare.

Exploring big responses with the tree view

An API response with hundreds of nested items is hard to read even when beautified. Switch to Tree view to fold and unfold objects and arrays, and hover any value to see its path, such as $.users[0].email. That path is exactly what you need when writing code that reads the value.

A warning about very long numbers

JavaScript cannot store integers with 16 or more digits exactly; 9007199254740993 quietly becomes 9007199254740992. That matters for IDs from some databases and social platforms. The formatter warns you when it sees such numbers, and the usual fix is for the API to send them as strings.

Related tools for developers

If a JSON value is a Base64 blob, decode it with the Base64 Encoder & Decoder, which detects whether it is text or a file. If it is a percent-encoded URL, the URL Encoder & Decoder turns it back into something readable and lists every query parameter.

Where broken JSON usually comes from

  • Copying from JavaScript code. Object literals in JS allow unquoted keys, single quotes and trailing commas; JSON does not.
  • Printing from Python. print(my_dict) gives single quotes and True/None. Use json.dumps(my_dict) instead.
  • Hand-editing config files. Adding an item to the end of a list and forgetting to add a comma to the line above it is the classic.
  • Chat apps and word processors. They can turn straight quotes into curly “smart” quotes, which JSON does not recognize. Paste code through a plain-text editor, or into the formatter directly.
  • Truncated responses. If a log or terminal cut a long response short, the error appears at the very end, usually "unexpected end of input". The fix is to get the full response, not to edit it.

Quick answers

Why is a trailing comma invalid?

The JSON standard does not allow a comma after the last item, even though JavaScript does.

Can JSON have comments?

No. Standard JSON does not allow them.

Is it safe to paste API keys?

Yes. The JSON is processed in your browser and never sent to a server.

More guides

All guides