Toolvil Guides

How to Format and Validate JSON

Has your JSON ever been squeezed onto one long line that is hard to read, or failed to parse with an 'Unexpected token' error? Once you know a few core rules of JSON syntax, you can find the cause yourself and format and validate it on your own.

Last updated 2026-06-16

What JSON is and why its syntax is so strict

JSON (JavaScript Object Notation) is a format for exchanging data as human-readable text. It is used almost everywhere: API responses, configuration files, webhook payloads and logs. But JSON has very strict syntax: a single misplaced comma or quote makes the whole parse fail. 'Close enough' does not work, and that is what surprises people most when they first work with it.

The key point is that JSON 'looks like' a JavaScript object but 'is not the same'. Many notations that JavaScript accepts are errors in JSON. That is why copying an object from a console and pasting it as JSON usually fails. Understanding this difference is the starting point for fixing JSON errors yourself.

Core JSON syntax rules you must follow

JSON values can only be one of six types: object ({}), array ([]), string, number, boolean (true/false) and null. The rules to follow on top of that are summarized in the table below.

RuleCorrect exampleIncorrect example
Wrap keys in double quotes (" ")"name": "철수"name: "철수"
Strings must also use double quotes only"city": "서울"'city': '서울' (single quotes)
No comma after the last item["a", "b"]["a", "b",] (trailing comma)
Comments are not allowed(no comments)// 설명, /* ... */
undefined and functions cannot be valuesUse nullundefined, NaN

As the table shows, the most common mistakes are all things that work in JavaScript but not in JSON. In short, always use double quotes for keys and strings, drop the last comma, and never write comments. Getting these three habits right removes most errors.

Single quotes and double quotes

In JavaScript, '서울' and "서울" are both strings, but JSON accepts double quotes only. Every value wrapped in single quotes is an error. The same error appears when text copied from a word processor or spreadsheet has curly quotes instead of plain double quotes ("), so check that the quotes are the plain kind even when they look similar.

Trailing commas

A comma after the last item of an array or object is not allowed in JSON. Modern JavaScript accepts trailing commas, so it is easy to add one out of habit, but the parse fails on the spot once the data is moved to JSON. Leaving a lone comma behind after deleting an item is another common cause.

Common errors and how to diagnose them

A JSON parser usually reports errors in the form Unexpected token ... in JSON at position N. Here position N is roughly the character position where the problem starts, so looking around it narrows down the cause quickly. These are the patterns you will meet most often.

  • Trailing comma: check whether a comma is left right before } or ]. This is the most common cause.
  • Missing key quotes: a key without double quotes, as in { name: "값" }, is an error. Change it to { "name": "값" }.
  • Single quotes: replace every ' with ".
  • Unbalanced brackets: check that the number of opening { and [ matches the closing } and ]. Adding indentation makes this visible at a glance.
  • Comments left in: remove any // or /* */ that was added as an explanation.

Counting error positions by hand is tedious, so it is usually faster to add indentation (format) first and read the expanded structure. When JSON that was squeezed onto one line is spread out line by line, you can see at once which line has the misplaced comma or quote. Paste it into the JSON formatter & viewer and it formats the text and marks the error position, and it can fix the common mistakes above automatically in one step.

Telling format (beautify) and minify apart

The two conversions you use most often with JSON are 'format' and 'minify'. Both leave the data itself unchanged and only change the whitespace, so pick the one that fits the purpose.

TypeWhat it doesWhen to use it
Format (beautify)Adds indentation and line breaks so it is easy to readWhen a person reads and debugs it, in code review
MinifyRemoves all whitespace and line breaks to make one lineTo reduce transfer and storage size, for URL parameters

Indentation is usually 2 or 4 spaces. Adding 'sort keys' arranges the keys of each object alphabetically, which helps when you compare two JSON files or look for changes. Sorting changes only the order of object keys and keeps the order of array items as it is, because the order of an array often carries meaning.

Working with deeply nested structures

When JSON is nested deeply and it is hard to find where a value is, a tree view helps. You can collapse and expand objects and arrays, scan the whole structure first and open only the parts you need. The location of a value is written as an access path such as data.users[0].email, and once you know the path you can read that value directly in code.

Handling it safely: check where it is processed

JSON often contains sensitive values such as API keys, tokens and personal data. When you use an online tool, it is best to check whether the data you enter is sent to a server or processed only inside your browser. A tool that processes locally in the browser does not send the input anywhere, so even sensitive JSON can be handled with relatively little worry.

If you still plan to share it, get into the habit of deleting values such as tokens and passwords first or replacing them with dummy values. Formatting and validation only help with syntax and display. They do not replace data security or schema (field structure) validation.

Common sticking points

What does an 'Unexpected token' error mean?

It means the parser found a character that cannot appear at that position. The cause is usually a comma or quote missing just before it, or a trailing comma left behind. Look first at the area the position number in the message points to. If you add indentation and spread the structure out, the line with the cause becomes much easier to see.

Can I use a JavaScript object as JSON as it is?

Mostly no. A JavaScript object may have keys without quotes and may use single quotes, trailing commas, comments and undefined, but JSON forbids all of them. To get valid JSON, change keys and strings to double quotes, remove trailing commas and comments, and replace undefined with null.

My non-English text looks garbled. Is the JSON wrong?

It is usually an encoding (character display) problem, not a syntax problem. JSON can hold Unicode as it is, so make sure the file is saved as UTF-8 and opened with the same encoding. If the quotes have been changed to curly quotes instead of plain double quotes ("), that is a syntax error and you need to replace them with plain quotes.