Understanding JSON

A look at one of the most widely used data interchange formats.

JSON, short for JavaScript Object Notation, is a text-based format for representing structured data. It's become one of the default ways systems pass data back and forth, especially on the web. Here's how it's structured and what kinds of data it can hold. There's a JSON formatter on this site if you want to tidy up or check a snippet as you go.

Origins and purpose

JSON borrowed its look from the way objects are written in JavaScript, but it long ago outgrew that origin and now lives happily in just about every language, tied to none of them. The whole point is a format that's easy for people to read and easy for computers to chew through. Being plain text, it travels over networks and sits in files without any fuss.

Basic structure

Almost everything in JSON comes down to two structures. The first is an object, wrapped in curly braces, holding a set of name and value pairs. Each name is a text string, followed by a colon and its value, and pairs are separated by commas. The second is an array, wrapped in square brackets, holding an ordered list of values separated by commas. Nest these inside one another and you can describe data of just about any shape.

Data types

The set of value types is deliberately small. There are strings, which are text in double quotation marks; numbers, whole or decimal; the booleans true and false; and null, which stands for the absence of a value. A value can also be another object or array, and that's exactly how nested structures come about. Keeping the list of types short is part of what makes JSON so simple and predictable to work with.

Common uses

The most familiar use is moving data between a browser and a server. When an app asks for something, the server typically answers with JSON, and the app reads it from there. Beyond that, JSON turns up in configuration files, in stored data, and as the format that countless APIs both accept and hand back. That near-universal support across languages and tools is a big part of why people keep reaching for it.

Common errors

JSON's syntax is strict, so a small slip can render the whole thing invalid. The usual suspects are a missing or extra comma, single quotation marks where strings need double ones, and a trailing comma left after the last item in an object or array. A formatter or validator earns its keep here by pointing right at the spot where the structure stops matching what JSON expects.

Summary

In short, JSON is a text-based format built from objects and arrays, with a small, tidy set of value types. It's used everywhere to exchange and store structured data across more or less every language. That simple, strict syntax is what makes it easy to process, and also why a small formatting mistake can be enough to break it.

Try the JSON formatter · Back to all articles