📝 Tutorials

What is JSON? A Simple Explanation With Examples


JSON (JavaScript Object Notation) is a text format for storing and exchanging data. It’s how computers send data to each other over the internet — when you load a website, the API responses are almost always JSON.

It looks like this:

{
  "name": "Alice",
  "age": 30,
  "developer": true,
  "languages": ["JavaScript", "Python", "Go"],
  "address": {
    "city": "Brussels",
    "country": "Belgium"
  }
}

That’s it. If you can read the above, you can read JSON.

The rules

JSON has very few rules, which is why it’s so popular:

  1. Data is in key-value pairs: "name": "Alice"
  2. Keys must be strings in double quotes: "name" not name or 'name'
  3. Values can be: string, number, boolean, null, array, or object
  4. Strings use double quotes: "hello" not 'hello'
  5. No trailing commas: the last item can’t have a comma after it
  6. No comments: JSON doesn’t support comments (this annoys everyone)

Data types

{
  "string": "Hello world",
  "number": 42,
  "decimal": 3.14,
  "boolean": true,
  "nothing": null,
  "array": [1, 2, 3],
  "object": {
    "nested": "value"
  }
}

That’s every type JSON supports. No dates, no functions, no undefined.

Common mistakes

// ❌ Single quotes
{'name': 'Alice'}

// ✅ Double quotes
{"name": "Alice"}

// ❌ Trailing comma
{"name": "Alice", "age": 30,}

// ✅ No trailing comma
{"name": "Alice", "age": 30}

// ❌ Comments
{
  "name": "Alice" // this is a comment
}

// ✅ No comments allowed in JSON
{
  "name": "Alice"
}

Reading JSON in JavaScript

// Parse JSON string → JavaScript object
const data = JSON.parse('{"name": "Alice", "age": 30}');
console.log(data.name);  // "Alice"

// JavaScript object → JSON string
const json = JSON.stringify({ name: "Alice", age: 30 });
console.log(json);  // '{"name":"Alice","age":30}'

// Pretty print
const pretty = JSON.stringify(data, null, 2);

Reading JSON in Python

import json

# Parse JSON string → Python dict
data = json.loads('{"name": "Alice", "age": 30}')
print(data["name"])  # Alice

# Python dict → JSON string
text = json.dumps({"name": "Alice", "age": 30})

# Pretty print
text = json.dumps(data, indent=2)

# Read from file
with open("data.json") as f:
    data = json.load(f)

# Write to file
with open("data.json", "w") as f:
    json.dump(data, f, indent=2)

Where you’ll see JSON

  • API responses: almost every web API returns JSON
  • Config files: package.json, tsconfig.json, VS Code settings
  • Databases: MongoDB stores JSON documents, PostgreSQL has a JSONB type
  • Data exchange: any time two systems need to share structured data

JSON vs. YAML vs. XML

JSONYAMLXML
ReadabilityGoodBestWorst
CommentsNoYesYes
Data typesBasicRichStrings only
Used forAPIs, configConfig, CI/CDLegacy systems

JSON won the API world because it’s simple, fast to parse, and works natively in JavaScript. YAML is preferred for config files because it supports comments and is easier to read.

See also: YAML cheat sheet and the JSON Formatter tool for pretty-printing and validating JSON.