JSON Formatter

A free browser tool that formats, validates and minifies JSON. Paste any snippet and see at once whether it is valid and where it breaks, with an error hunt, a worked example and ideas for class.

Web toolBachelor +2CC BY 4.0

What you will learn

  • Check whether a snippet is valid JSON
  • Read nested JSON once it is formatted
  • Find and fix the five most common JSON syntax errors
  • Explain what formatting and minifying change and what they keep

JSON (JavaScript Object Notation) is a plain-text format for structured data: objects in curly brackets { } hold "key": value pairs, arrays in square brackets [ ] hold lists. Most web services send their answers as JSON, often as one long line that nobody can read. The formatter turns that line into an indented tree.

What it does

  • Formats JSON with indentation so nested objects and arrays become readable. It formats as you type.

  • Says whether the text is valid JSON, and where it breaks if it is not.

  • Minifies it back to one line.

  • Lets you choose the indentation: 2 spaces, 4 spaces or a tab.

  • Copies the result, or downloads it as a file called formatted.json.

  • Has a Sample button that loads a short example, and Clear to start again.

It runs in the browser: what you paste is not uploaded, and no account is needed.

The error message comes from the browser itself, so its wording differs between Chrome, Firefox and Safari. Most name the place as a line and column or as a character position, for example "Expected double-quoted property name in JSON at position 16 (line 1 column 17)".

The rules it checks

The tool checks strict JSON, the standard every web service relies on. The five rules learners break most often:

  1. Keys and text values need double quotes: "name": "Anna", never name: 'Anna'.

  2. No comma after the last item of an object or array: [1, 2, 3], not [1, 2, 3,].

  3. true, false and null are written in lower case.

  4. No comments. // note is not allowed anywhere, even though some configuration files accept it.

  5. Numbers are plain: no leading zeros (7, not 007), no .5 without the zero, no NaN.

A single value such as 42 or "hello" is valid JSON on its own, and so is any text with accents or other alphabets, such as "city": "Köln".

A worked example

This snippet has three errors. Paste it into the tool and fix them one at a time; the message changes as each one is fixed.

Find three errors
{
  "company": "Northwind Traders",
  "city": 'Hamburg',
  "active": True,
  "contacts": ["anna.berger@northwind.example", "sam.reed@northwind.example",]
}
Solution
  1. 'Hamburg' uses single quotes. Write "Hamburg".

  2. True has a capital T. Write true.

  3. There is a comma after the last address in the array. Delete it.

Fixed
{
  "company": "Northwind Traders",
  "city": "Hamburg",
  "active": true,
  "contacts": ["anna.berger@northwind.example", "sam.reed@northwind.example"]
}

Minified, the fixed version is one line of 136 characters instead of six lines.

What formatting changes, and what it does not

Formatting and minifying only move spaces and line breaks. Keys keep their order and values keep their meaning. A few details do change, because the text is read and written again:

  • If a key appears twice in one object, only the last value is kept.

  • 1.0 becomes 1 and 1e3 becomes 1000: the same number, written differently.

  • Very long whole numbers (more than about 15 digits, such as some ID numbers) lose their last digits. Real services send such IDs as text in quotes for exactly this reason.

Common mistakes

  • Pasting a JavaScript object from code instead of JSON. JavaScript allows single quotes and keys without quotes; JSON does not.

  • Copying "smart" curly quotes from a word processor. “name” is not "name".

  • Pasting two JSON objects one after the other. That is two documents; wrap them in an array: [{...}, {...}].

  • Looking for the error only at the reported position. The real mistake is often just before it, such as a missing comma at the end of the previous line.

Ideas for class

  • Paste the snippets from the JSON basics exercise and let the tool confirm each answer.

  • Open a public API response (for example an open data portal) and let learners find one value in the formatted result.

  • Error hunt: each learner writes a valid snippet about themselves (name, hobbies as an array, one nested object), then breaks it with one of the five errors above. A partner finds the error with the tool.

  • Format and minify: compare the length of the same data formatted and minified. Why do web services send the minified version?

Quiz

Valid or not?

4 questions
  1. 1.Is { "price": 4.90, "currency": "EUR" } valid JSON?

  2. 2.Is { 'price': 4.90 } valid JSON?

  3. 3.Is ["red", "green", "blue",] valid JSON?

  4. 4.Is { "active": null } valid JSON?

The tool is free to use. This description is licensed under CC BY 4.0.

Reuse this material

CC BY 4.0

You may copy, adapt and share it, also commercially, as long as you credit ManyPI and link back.

"JSON Formatter" by ManyPI (https://manypi.com/edu/json-formatter), licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)