JSON Viewer, Formatter & Validator · Tutorial
How to fix invalid JSON from an API response
6 min readbeginnerUpdated
You copied JSON from an API, a log file or a colleague's message, and your code refuses to parse it. This tutorial shows how to use the JSON Formatter to find each problem by line and column, understand what is wrong, and fix it until the document is valid and neatly formatted.
What you need
- The JSON Viewer, Formatter & Validator
- The broken JSON you want to fix (or use the sample from the Try it button)
Steps
Paste the broken JSON
Open the formatter and paste your JSON into the left box. If the data is in a file, use Open a .json file or drag the file onto the drop area. The tool checks the input as soon as it arrives, so there is no button to press.
Read the first error
When the JSON is invalid, the status bar turns red and shows a message such as
Line 4, column 5: Property names must be in double quotes. The checker always reports the first problem it meets, reading from the top. Later errors will appear once you fix the earlier ones, so work from top to bottom.Jump straight to the problem
Press Go to error. The cursor moves to the exact character in the left box, which saves scrolling through hundreds of lines. In the sample, the first stop is
nameon line 4, which needs double quotes around it.name: "Priya Shah", ← invalid "name": "Priya Shah", ← validFix trailing commas and missing commas
The next errors in the sample are a comma after the last property in
customerand a missing comma between the two items in theitemsarray. JSON allows a comma only between values, never after the last one. Delete the extra comma and add the missing one. The message tells you which case you are looking at.Replace single quotes and non-JSON values
'status'uses single quotes, andFalseis written the way Python writes it. JSON accepts only double-quoted strings and lowercasetrue,falseandnull. Change them to"status"andfalse.Check the result and copy it
When the status bar turns green and reads Valid JSON, the right box shows the formatted document. Press Copy to put it on your clipboard, or Download .json to save a file. Turn on Sort keys first if you want properties in alphabetical order.
Try it yourself
Opens the JSON Viewer, Formatter & Validator with this tutorial's sample loaded.
Common mistakes
- Fixing errors out of order
- The position of later errors can shift while earlier ones are still broken. Always fix the error currently shown, then let the tool re-check.
- Copying JSON out of a JavaScript file
- Code like
const data = { ... };is a JavaScript object, not JSON. Removeconst data =and the final semicolon, then fix any unquoted keys the checker reports. - Smart quotes from a word processor or chat app
- Curly quotes such as “ and ” look like double quotes but are different characters. If the error points at a quote that looks correct, retype it as a plain
". - Hidden text around the JSON
- Log lines often add a timestamp or prefix before the opening
{. An Unexpected character error at line 1, column 1 usually means there is extra text to remove.
Next steps
Go back to the JSON Viewer, Formatter & Validator, or keep going with one of these:
FAQ
Can the tool fix the errors automatically?
Yes. When the JSON is invalid, a Fix automatically button appears next to Go to error. It repairs common mistakes such as quotes, commas and Python-style values in one step. Always check the result, because a guess about missing data can be wrong. Fixing by hand, as shown here, means you see every change.
Why does the error point one line below the real problem?
A missing comma is only noticed when the next value starts, so the position is the start of that value. Look at the end of the line just above it.