HSHimanshu Sharma
Free Tools

JSON explained: validate, format and fix JSON with the JSON Formatter

Advertisement

JSON is the language that applications use to send data to each other. When a website loads your messages, when a phone app shows the weather, when a tool reads its settings from a file, there is a good chance the data travelled as JSON. As a developer you will read it constantly, and when it breaks, the error message is often unhelpful. The free JSON Formatter validates JSON, makes it readable with indentation, compacts it for sending, and explains what is wrong when it is invalid. This guide covers the rules of JSON, its most common errors, and how to work with it comfortably.

What JSON is

JSON stands for JavaScript Object Notation. It began as a way to write JavaScript objects as text, but it is language independent, and nearly every programming language can read and write it. It is popular because it is small, simple and easy for people to read.

A JSON document is built from just a few pieces:

  • Objects, written in curly braces, holding named values: {"name": "Asha"}
  • Arrays, written in square brackets, holding ordered lists: [1, 2, 3]
  • Strings, in double quotes: "hello"
  • Numbers: 42, -3.5, 1e6
  • Booleans: true and false
  • null: null

That is the entire language. Here is a small example with all of those together:

{
  "name": "Asha",
  "age": 31,
  "active": true,
  "manager": null,
  "tags": ["admin", "editor"],
  "address": { "city": "Delhi", "postcode": "110001" }
}

The rules of valid JSON

JSON looks like a JavaScript object, but it is stricter. Many errors come from writing JavaScript syntax and expecting it to be accepted.

  1. Keys must be strings in double quotes. {name: "Asha"} is not valid JSON, though it is valid JavaScript.
  2. Strings use double quotes only. 'Asha' with single quotes is not allowed.
  3. No trailing commas. [1, 2, 3,] and {"a": 1,} are errors.
  4. No comments. Neither // note nor /* note */ is allowed.
  5. Only these value types. There is no undefined, no functions, no dates. Dates are usually written as strings, such as "2026-10-22T10:30:00Z".
  6. Numbers must be written plainly. No leading zeros (007 is invalid), no hexadecimal, no NaN or Infinity, and no plus sign at the start.
  7. Special characters in strings must be escaped. A double quote inside a string is written \", a backslash as \\, a new line as \n.
  8. One top-level value. A document is a single object, array, string, number, boolean or null.

Keep this list in mind and you will recognise most problems on sight.

The errors you will meet most often

A trailing comma

{
  "name": "Asha",
  "age": 31,
}

The comma after 31 has nothing after it. Remove it. This is the most common mistake, especially when you delete the last item of a list and forget the comma before it.

Single quotes

{ 'name': 'Asha' }

Replace every single quote with a double quote. If the text contains an apostrophe, it can stay inside a double-quoted string without escaping.

Unquoted keys

{ name: "Asha" }

Put the key in double quotes.

A missing comma between items

{
  "name": "Asha"
  "age": 31
}

Every item except the last needs a comma after it.

A missing closing bracket

If the document ends early, you have an opening { or [ without its match. This is common in long documents that were cut off when copied. Formatting the document with indentation makes the unbalanced part easy to spot, because the indentation does not return to the left edge where it should.

Comments

Configuration files written by hand often include notes. Standard JSON does not allow them. Remove the comments, or store the note as a value such as "_note": "text". Some tools accept a relaxed variant that allows comments, but you cannot rely on that for data you send to other systems.

Special characters inside strings

A Windows path such as "C:\Users\Asha" is invalid, because \U is not a valid escape. It must be "C:\\Users\\Asha". A line break inside a string must be written \n, not typed as a real new line.

Using the JSON Formatter

  1. Open the JSON Formatter.
  2. Paste your JSON into the input box.
  3. Choose Format to pretty-print it with indentation, or Minify to remove all unnecessary spaces and line breaks.
  4. If the JSON is invalid, read the error message. It explains what the parser found at the point where it stopped.

A useful detail about error messages: a parser reports the place where it noticed the problem, which is not always where the mistake is. A missing comma is detected when the parser meets the next item. A missing closing quote may be reported far later, at the end of the document. When the message points to a line that looks right, check the line before it.

Here is what formatting does. Compact JSON like this:

{"name":"Asha","tags":["a","b"]}

becomes:

{
  "name": "Asha",
  "tags": [
    "a",
    "b"
  ]
}

The data is identical. Only the white space changed, and white space between items has no meaning in JSON.

When to format and when to minify

Format while you are reading, debugging or editing. Indentation shows the structure at a glance, so you can see which values belong to which object.

Minify before sending data over a network or storing it somewhere that charges for size. Removing white space makes the document smaller, though the saving is modest compared with compressing the response, which web servers usually do.

Working with JSON in code

Every popular language has a built-in way to turn JSON text into data and back. The two operations are usually called parse and stringify or decode and encode.

In JavaScript:

const data = JSON.parse('{"name":"Asha","age":31}');
console.log(data.name);            // Asha
console.log(JSON.stringify(data)); // {"name":"Asha","age":31}
console.log(JSON.stringify(data, null, 2)); // pretty-printed with 2 spaces

In Python:

import json
data = json.loads('{"name": "Asha", "age": 31}')
print(data["name"])            # Asha
print(json.dumps(data, indent=2))

In PHP, json_decode() and json_encode() do the same job, and the JSON lesson in the PHP course shows how to handle errors safely. Always handle a failure when you parse data you did not write, because the input can be invalid.

Tips for working with JSON

Validate before you trust. If data comes from a user, a file or another system, parse it inside error handling and check that the fields you need exist and have the right type.

Be consistent with naming. Choose camelCase or snake_case for keys and keep to it. The Case Converter turns a list of names from one style to another.

Keep numbers in mind. JSON has one number type. Very large integers can lose precision when parsed by systems that use floating-point numbers, so identifiers longer than about 15 digits are safer as strings.

Do not put secrets in JSON that you share. When you paste data into any website to debug it, remove tokens, passwords and personal details first.

Prefer arrays for ordered lists and objects for named things. Using an object whose keys are "0", "1", "2" is a sign that you wanted an array.

Related tools

When JSON contains Base64 data, decode it with the Hash & Encode Toolkit. Unique identifiers for records are easy to create with the UUID Generator. Dates inside JSON are often Unix timestamps; the Unix Timestamp Converter turns them into readable dates. To compare two versions of a JSON document and see what changed, use the Text Diff Checker.

Quick answers

Is JSON the same as a JavaScript object? No. They look similar, but JSON is a text format with stricter rules, and a JavaScript object can hold functions and other values that JSON cannot.

Why can't I put comments in JSON? The format was designed to be minimal and unambiguous. Comments were deliberately left out.

Does the order of keys matter? In an object, key order has no meaning in the standard, so do not depend on it. An array's order does matter.

Can JSON be huge? Yes, but very large documents are better handled by tools that read them in pieces. A browser-based formatter is best for documents of reasonable size.

Advertisement