HandyHex.Handy tools in every shape.
GUIDE · 3 MIN READ

JSON Basics and the Errors Everyone Makes

What JSON is, its syntax rules, and how to find and fix the most common mistakes such as trailing commas and single quotes.

What JSON is

JSON (JavaScript Object Notation) is a lightweight text format for structured data. Web APIs use it to send information between servers and apps, and many configuration files and databases store it. Despite the name, it is language-independent, and almost every programming language can read and write it. Its popularity comes from being simple for people to read and easy for programs to parse.

The building blocks

JSON has just six kinds of value: an object in curly braces holding key–value pairs, an array in square brackets holding an ordered list, a string in double quotes, a number, the booleans true and false, and null. Objects and arrays can nest inside each other to any depth. A small example: {"name":"Ana","age":31,"tags":["admin","editor"]}.

The strict rules

JSON is stricter than JavaScript object syntax. Keys must be in double quotes. Strings must use double quotes, never single quotes. No comma may follow the last item. Comments are not allowed. Numbers cannot have leading zeros or a trailing decimal point, and values such as NaN and undefined do not exist. Break any rule and a parser rejects the whole document.

The most common errors

Trailing comma: {"a":1,} is invalid. Remove the final comma. Single quotes: {'a':1} is invalid. Switch to double quotes. Unquoted keys: {a:1} is invalid. Missing comma between items, a frequent mistake after copy-pasting lines. Unescaped characters inside strings, such as a double quote or a backslash, which need \" or \\, and raw line breaks, which need \n. Mismatched brackets, where the number of { and } differs.

Paste your text into the JSON Formatter and press Format. If it is valid you get a cleanly indented version, which also makes brackets easy to match. If not, the error message names the character position where the parser gave up. The real mistake is often just before that point, for example a missing comma on the previous line.

Formatting versus minifying

Indented JSON is for humans. Minified JSON removes all optional whitespace and is smaller, which is useful when sending data over a network or embedding it in a file. The two forms are equivalent to a parser. Use format for reading and debugging, and minify for production payloads.

Converting from spreadsheets

Data often starts life in a spreadsheet. Exporting it as CSV and converting with the CSV to JSON tool turns each row into an object, keyed by the header names. Note that CSV has no data types, so every value arrives as text, such as "31" rather than 31, and the converter does not handle commas inside quoted fields. After converting, validate the output in the formatter, and convert numbers or booleans in your own code if your application needs them.

Reading nested JSON

Real responses nest objects inside arrays inside objects. Read from the outside in. Each opening brace or bracket starts a level, and the matching closing one ends it. After formatting with the JSON Formatter, the indentation shows the depth. To reach a value in code you follow a path, such as data.users[0].name: the data object, its users array, the first item, then its name.

If a response is huge, collapse levels in your editor or look at the keys of the top level first. Missing keys are common in real data, so code should check for them instead of assuming they exist.

JSON compared with CSV and YAML

CSV is flat and compact, ideal for tables with identical columns, but cannot represent nested data. JSON handles nesting and is the standard for web APIs. YAML is easier for humans to write and allows comments, so it is common for configuration, but its flexible syntax and indentation rules cause subtle mistakes. Choose the simplest format that fits your data.

Going further: validating structure

Valid JSON only means correct syntax. It does not mean the data makes sense. A JSON Schema is a separate document that describes what an object must contain, such as which keys are required and which values must be numbers. Validators check data against a schema and give clear messages like 'age must be a number'. Use one whenever other people or programs send data to you, so mistakes are caught early. Also remember that numbers above about 15 digits can lose precision when parsed, so large identifiers are best stored as strings.

Try the tools