JSON is a way of writing data down as text so that one program can hand it to another. It
was taken from the way JavaScript writes objects, but it is stricter than JavaScript, and
nearly every programming language can now read and write it. API responses, configuration
files and log lines are very often JSON.
What it is made of
A JSON document is a single value, and there are six kinds of value. An object is a list
of keys and values inside braces, where every key is a string. An array is a list of
values inside square brackets. A string is text inside double quotes, a number is written
in decimal, and the last three are the words true, false and
null. Objects and arrays can hold any kind of value, including more objects
and arrays, which is how JSON describes things that have parts.
On one line, which is called minified
{"name":"Ada","languages":["en","fr"],"active":true}
Formatted with two spaces of indentation
{
"name": "Ada",
"languages": [
"en",
"fr"
],
"active": true
}
The spaces and line breaks between the parts mean nothing to a program reading JSON. The
same document can be written on a single line to make it as small as possible, or spread
over many lines with indentation so that a person can read it. Formatting only changes
that whitespace, so the two versions above hold exactly the same data.
What JSON does not allow
Most JSON that will not parse has one of a handful of problems, usually because it was
written by hand or copied out of JavaScript or Python, which are both more forgiving. This
page recognizes each of these, and the status line names the one it found.
A comma after the last item in an object or an array. JavaScript accepts
[1, 2, 3,], but JSON does not.
Comments, whether they start with // or are wrapped in /* and */.
Single quotes. Strings and keys both need double quotes, and they have to be straight
ones rather than the curly quotes that a word processor or a chat app puts in.
Keys without quotes, such as {name: "Ada"}, which JavaScript accepts and JSON does not.
Words that other languages use, such as Python's None and
True or JavaScript's undefined and NaN. JSON only
has null, true and false, always in lower case.
A line break or a tab typed directly inside a string. They have to be written as the
escapes \n and \t, and a backslash that is meant as itself, as
in a Windows path, has to be written twice.
Numbers with a leading zero like 007, a plus sign, or a decimal point with no digit before it.
Comments and trailing commas
Some files that look like JSON are really JSONC, a variant that allows comments and
trailing commas. The tsconfig.json file in a TypeScript project is one, and so
are the settings files of several code editors. When the only problems in what you paste
are comments and trailing commas, the status line offers to remove them, and it tells you
how many of each it took out.
Numbers are kept exactly as they were written
JSON itself puts no limit on how large or how precise a number can be, but most programs
that read it do. JavaScript, for example, keeps about 16 significant digits of any number,
so its JSON.parse reads the ID 12345678901234567890 as
12345678901234567000. Large IDs from databases are the usual victims. This
page never converts a number at all. It copies each one into the formatted version digit
for digit, and when a number is one that JavaScript would change, the status line points
it out.
Strings are treated the same way. An escape such as é stays an escape in
the formatted copy instead of being turned into the character it stands for, so the only
thing formatting changes is the whitespace, unless you ask for the keys to be sorted.
Duplicate keys
The JSON standard says the keys in an object should be unique, but it does not require
it, and parsers disagree about what to do when a key appears twice. Most keep the last
value without saying anything, a few keep the first, and some refuse the document. This
page keeps both, in the order they were written, and the status line tells you where they
are.
JSON inside a string
Sometimes a whole JSON document arrives inside a string, with every quote escaped, like
"{\"id\":1}". That usually means something encoded it twice, which happens a
lot in log files and message queues. When the string you paste contains JSON, the status
line offers to format the JSON inside it.
JSON Lines
A log file often has one JSON value on each line, which is a format called JSON Lines or
NDJSON. That is not a single JSON document, so it will not parse as one. When every line
is valid on its own, the status line says so and offers to put the lines into an array,
which is a single document that the page can format.
Try the example in the tool