Back to NiceJSON
NiceJSON Blog/ What Is JSON? A Beginner's Guide to JavaScript Object Notation

What Is JSON? A Beginner's Guide to JavaScript Object Notation

Learn what JSON is, how its syntax works, which data types it supports, and why it became the default data format for web APIs and configuration files.

3 min read
  • json
  • fundamentals
  • beginners

JSON in one sentence

JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and exchanging structured data. It is easy for people to read and easy for programs to parse, which is why almost every web API, configuration file, and NoSQL database speaks it.

Despite the name, JSON is language-independent. Python, Java, Go, Rust, C#, PHP, and nearly every other language ship with a JSON parser.

A first example

{
  "name": "Ada Lovelace",
  "age": 36,
  "isActive": true,
  "languages": ["English", "French"],
  "address": {
    "city": "London",
    "country": "UK"
  },
  "spouse": null
}

This single object shows every building block JSON offers.

The two structures

JSON is built from just two structures:

  • Objects: an unordered collection of key/value pairs wrapped in curly braces.
  • Arrays: an ordered list of values wrapped in square brackets.

Objects and arrays can nest inside each other to any depth, which is how JSON represents complex data.

The six data types

TypeExampleNotes
String"hello"Must use double quotes
Number42, -3.14, 1e6No separate integer and float types
Booleantrue, falseLowercase only
NullnullRepresents "no value"
Object{"a": 1}Keys must be strings
Array[1, 2, 3]Can mix types

Syntax rules you must follow

  1. Keys must be strings wrapped in double quotes.
  2. Strings use double quotes. Single quotes are invalid.
  3. No trailing commas after the last item.
  4. No comments.
  5. No undefined, NaN, Infinity, or functions.

Breaking any of these rules produces a parse error. If you are not sure why your data fails to parse, paste it into the NiceJSON validator to see the exact line and column of the problem.

Why JSON won

  • Readable: you can understand a payload at a glance.
  • Compact: lighter than XML for the same data.
  • Native to the web: browsers parse it with JSON.parse() and produce it with JSON.stringify().
  • Universal: supported in every mainstream language and tool.

Parsing and generating JSON in JavaScript

const text = '{"name":"Ada","age":36}';
const obj = JSON.parse(text);   // string -> object
console.log(obj.name);          // "Ada"
 
const back = JSON.stringify(obj, null, 2); // object -> pretty string
console.log(back);

Next steps

Once you know the basics, learn how to format and beautify JSON for readability and how to find and fix the most common syntax errors. NiceJSON lets you do both in your browser without installing anything.