Guides Security and developers

JSON for Beginners: Syntax, Common Errors and Formatting

JSON's building blocks, the strict rules that cause most errors and how to format, validate and convert JSON data.

JSON, short for JavaScript Object Notation, is the most common format for exchanging data on the web. When a mobile app loads your messages, a website fetches prices or a payment gateway confirms a transaction, the data usually travels as JSON. It is also used for settings files, data exports and much of the data you will meet if you learn programming or work with no-code tools.

JSON is simple, but it is strict: a single misplaced comma makes a whole file invalid. This guide explains the building blocks, the rules that cause most errors and how to format, check and convert JSON.

What JSON looks like

Here is a small JSON document describing a person:

{
  "name": "Ayesha Khan",
  "age": 27,
  "city": "Lahore",
  "verified": true,
  "skills": ["Excel", "Python", "Urdu typing"],
  "address": {
    "street": "12 Canal View",
    "postcode": "54000"
  },
  "manager": null
}

Even without knowing JSON, you can probably read it. That readability, for people and for programs, is the reason it became so popular.

The building blocks

JSON has just six kinds of value:

TypeExampleNotes
String"Lahore"Text in double quotes
Number27, 3.14, -5, 1.5e3No quotes, no leading zeros
Booleantrue, falseLowercase only
NullnullMeans "no value"
Array["Excel", "Python"]An ordered list in square brackets
Object{"city": "Lahore"}Name and value pairs in curly braces

Objects and arrays can contain each other to any depth. In the example, "address" is an object inside the main object, and "skills" is an array of strings.

The rules that cause most errors

JSON looks like JavaScript, but its rules are stricter. These are the mistakes behind most "invalid JSON" messages:

  1. Names must be in double quotes. A name written as {name: "Ali"} or {'name': 'Ali'} is invalid. It must be {"name": "Ali"}.
  2. Strings must use double quotes. Single quotes are not allowed anywhere.
  3. No trailing commas. {"name": "Ali",} fails because of the comma before the closing brace. The same applies at the end of arrays.
  4. Commas between items. {"a": 1 "b": 2} fails because a comma is missing.
  5. Lowercase literals. true, false and null must be lowercase; True or NULL are invalid.
  6. No comments. Standard JSON has no comment syntax, so lines starting with // or wrapped in /* */ break it.
  7. No leading zeros. A number written as 012 is invalid. If the leading zero matters, as in a phone number or postcode, store it as a string: "012".
  8. Special characters in strings must be escaped. A double quote inside a string is written as \", a backslash as \\ and a new line as \n.

The JSON Formatter checks your data and points to the line and column of the first error, which is usually enough to spot the problem.

Formatting versus minifying

The same data can be written in two ways:

  • Formatted (pretty-printed) JSON uses line breaks and indentation, as in the example above. It is easy for people to read and compare.
  • Minified JSON removes all unnecessary spaces and line breaks, putting everything on one line. It is smaller and faster to send.

The example document is 238 bytes when formatted with two-space indentation and 176 bytes when minified, about 26 per cent smaller. For large files sent millions of times, that saving matters; for files people read and edit, formatting matters more. The JSON Minifier and the formatter convert in either direction.

Reading large JSON files

API responses and exports can run to thousands of lines. Rather than scrolling, use a tree view such as the JSON Viewer, which lets you collapse and expand objects and arrays. Two habits help:

  • Find the shape first. Is the top level an object or an array? What are the main keys?
  • Look at one item closely. In an array of 500 orders, understanding one order explains the other 499.

Comparing two versions

When an API response changes or a settings file stops working, comparing the old and new versions often reveals the cause. The JSON Diff tool compares structure and values rather than raw text, so differences in spacing or key order do not hide the real changes.

JSON and spreadsheets

Spreadsheets hold rows and columns; JSON can hold nested structures. Converting between them works best when the JSON is a flat array of similar objects. The JSON to CSV tool turns such an array into rows that open in Excel or Google Sheets, and CSV to JSON goes the other way. Nested objects are usually flattened into column names such as address.street.

Numbers, dates and precision

JSON has no date type. Dates are usually stored as strings, ideally in the international format 2026-10-26 or with a time as 2026-10-26T14:30:00Z, which sorts correctly and is unambiguous. Very large whole numbers, such as long IDs, can lose precision in JavaScript, which uses double-precision numbers for all values; many APIs send such IDs as strings for that reason.

Fixing a broken file, one error at a time

Validators report the first error they meet, so fixing JSON is a loop: fix, check again, repeat. Take this product record, which contains three mistakes:

{
  "product": "Lawn suit",
  'price': 4500,
  "sizes": ["S", "M", "L",],
  "in_stock": True
}
  1. The first check points to line 3: the name 'price' uses single quotes. Change it to "price".
  2. The next check points to line 4: there is a trailing comma after "L" before the closing bracket. Remove it.
  3. The next check points to line 5: True is capitalised. Change it to true.
  4. The final check reports valid JSON.

Working through errors in order like this is faster than trying to spot every problem at once, especially in long files where one early mistake can make later lines look wrong too.

Where you will meet JSON

  • Web and mobile APIs, which send and receive data as JSON in almost every modern app.
  • Settings files for software, editors and build tools.
  • Data downloads from many online services, which often offer your account data as JSON files.
  • Automation tools, where steps pass data to each other as JSON.
  • Browser developer tools, where the Network tab shows the JSON a page loads, which is invaluable for debugging.

JSON, XML and YAML

JSON is not the only data format. XML uses opening and closing tags and is still common in older business systems, banking and government data exchanges. YAML uses indentation instead of brackets and is popular for configuration files, but its flexible rules can surprise beginners. Converting between formats is routine: the JSON to XML and YAML to JSON tools handle the most common cases.

Common mistakes

  • Copying JSON from a document editor. Word processors replace straight quotes with curly ones, which JSON does not accept.
  • Editing by hand and forgetting a comma. Validate after every edit.
  • Storing phone numbers or postcodes as numbers. Leading zeros disappear; use strings.
  • Assuming key order matters. JSON objects are unordered; programs should not depend on the order of keys.
  • Pasting sensitive data into unknown websites. API responses can contain personal information or access tokens.

Privacy

The JSON tools on this site process data inside your browser. API responses, configuration files and exports are never uploaded, which matters when they contain customer details, keys or tokens.

Free tools used in this guide