JSON Explained: The Format Behind Every API
JSON is how programs talk to each other. When your phone app asks a server for the weather, your messages, or your bank balance, the answer almost always comes back as JSON: a block of text made of braces, brackets, quotes, and commas. It looks messy, but the whole thing runs on six types of values and a few strict rules. Learn those, and you can read any API response—and fix the ones that break. This guide covers both, using one real example from start to finish.
The six kinds of values
Everything in JSON is one of six things. An object is a set of name and value pairs inside braces, like a row in a table. An array is an ordered list inside brackets. A string is text inside double quotes. A number is written plainly, with no quotes. A boolean is the word true or false, also with no quotes. And null means no value, on purpose. Objects and arrays can hold any of the six, including more objects and arrays, which is how one small format describes data of any shape.
{"name":"Ada Lovelace","role":"admin","active":true,"logins":128,"tags":["ops","dev"]} is one record that uses five of the six JSON data types. Read it left to right. The object holds five pairs. The name is a string. The role is a string. Active is a boolean, set to true. Logins is a number, 128. Tags is an array with two strings. That's a complete piece of JSON. Every response you'll ever see is the same pattern, just bigger: pairs, lists, and plain values, nested as deep as the data needs.
The rules that break JSON
JSON is strict, and that strictness is a good thing: a parser never has to guess. Object names must be strings in double quotes. Strings must use double quotes; single quotes are not allowed anywhere. Every pair is separated by a comma, and the last pair takes no comma after it. JSON has no comments at all. Numbers cannot carry a plus sign or a leading zero in front. Break any of these rules, and the document is not JSON, and every parser will refuse it.
The trailing comma after the last pair causes most failures. Plenty of other languages allow it, so hands type it out of habit. Single-quoted strings come next, then unquoted names, comments added to a config file, and text cut off mid-way by a copy that stopped early. When a document breaks, the fix is usually simple. Run it through a formatter that reports the exact spot of the first error. Fix that spot, then run it again. The first error is the only one that matters. The others are usually just its shadow.
Formatted or minified: same data, different job
Whitespace in JSON means nothing to a parser. Line breaks, indentation, and spaces after colons are all for human eyes. That one fact creates the format's two working forms. Pretty-printed JSON spreads the data over indented lines so a person can read the structure. Minified JSON removes every unneeded character so the data is as small as it can be for the trip between machines. The data is identical in both. Only the audience differs.
Our example record shows the size of the difference. Pretty-printed with two-space indentation, it takes 120 bytes. Minified, it takes 86 bytes. The 34 bytes saved are about 28 percent. The share grows with the document. Indentation repeats on every line. So the working rule is simple: format it pretty. You are reading, debugging, or storing it. A person will meet it again. Minify it. A machine is sending it to another machine. Every byte rides the network.
JSON and its neighbors
JSON's closest neighbor is YAML, the format of many config files. YAML trades braces and brackets for indentation, allows comments, and lets many strings go unquoted. That makes it friendlier to write by hand and easier to break by accident, because a shifted space changes the meaning. Converting between the two is a daily job when a config written as YAML must be sent to an API that speaks JSON, or when you want to check a YAML file by viewing it in JSON's stricter shape.
Two limits of JSON are worth knowing before they surprise you. JSON has no date type: dates. Times travel as strings, often ISO 8601 text. Or as numbers of seconds or milliseconds since an epoch. The two sides of a conversation must agree on which. And JSON carries data only, never behavior: no functions, no formulas, no references from one part of a document to another. Those limits are why it won. A format that does less, exactly the same way all over, is a format every language can read, and nearly every language ships with a parser built in.
A good way to work with JSON
Three habits will carry you through nearly every JSON job. First, never judge a response by its first screenful. Format it, and the structure becomes visible at once: which keys exist, which are missing, which hold the wrong kind of value. Most API surprises are not wrong data but data shaped differently than you assumed. A list appears where you expected one record. A number arrives as a string.
Second, when something breaks, fix the first error only and check again. Parsers report where they gave up, which is where the document stopped making sense, and that spot is usually within a line or two of the real mistake. Third, keep a known-good example of every document you depend on. When a response changes and your code fails, comparing the new shape against the saved one shows what moved in seconds. The tools on this site cover each of these jobs, in your browser, with your data staying on your device.
Try it yourself
The JSON tools on CodeToolWorks work with the same format. The JSON Formatter cleans up, checks, and points out the error in broken JSON. The JSON Minifier makes it smaller and tells you how many bytes it saved. The YAML to JSON Converter checks YAML files and changes them into JSON.