๐ŸชบToolNest
developer8 min read

What Is JSON? A Complete Beginner's Guide

JSON explained from the ground up โ€” syntax rules, data types, nested structures, comparison with XML, and common pitfalls to avoid.

TN

ToolNest Team

January 20, 2026

#JSON#data format#API#developer tools

What Is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format. It's human-readable, easy for machines to parse, and has become the de facto standard for data exchange on the web โ€” especially in REST APIs.

Despite the name, JSON is language-independent. Every major programming language has libraries to read and write JSON. The format was formalized in RFC 8259 and ECMA-404.

JSON Syntax Rules

JSON is built around six data types and two structures:

Primitive types:

  • String โ€” Text in double quotes: "hello"
  • Number โ€” Integer or float: 42, 3.14, -1, 1.5e10
  • Boolean โ€” true or false (lowercase only)
  • Null โ€” null (lowercase only)

Composite structures:

  • Object โ€” A collection of key-value pairs enclosed in {}
  • Array โ€” An ordered list of values enclosed in []

A complete JSON example:

{
  "name": "Jane Smith",
  "age": 29,
  "isActive": true,
  "address": {
    "street": "123 Main St",
    "city": "Austin",
    "state": "TX"
  },
  "tags": ["developer", "designer"],
  "profile_image": null
}

JSON Object Rules

  • Keys must always be double-quoted strings โ€” {"key": "value"} not {key: "value"}
  • Key-value pairs are separated by colons
  • Pairs are separated by commas
  • No trailing commas โ€” {"a": 1, "b": 2,} is invalid JSON

Nested Objects and Arrays

JSON supports arbitrary nesting. An array can contain objects, which can contain arrays, and so on:

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "roles": ["admin", "user"]
    },
    {
      "id": 2,
      "name": "Bob",
      "roles": ["user"]
    }
  ],
  "total": 2
}

JSON vs XML

JSON largely replaced XML for web APIs because of its simpler syntax and smaller size.

Feature JSON XML
Syntax Minimal, concise Verbose with open/close tags
Data size Smaller Larger (tag overhead)
Arrays Native support No native array type
Comments Not allowed Allowed
Schema JSON Schema (optional) XSD (complex but powerful)
Namespaces None Supported
Parsing speed Generally faster Generally slower

The same data structure takes roughly 30-40% more bytes in XML than in JSON.

XML still has advantages: it supports namespaces, document markup (mixing text and data), more mature tooling for complex schemas, and is widely used in enterprise/SOAP systems.

Parsing JSON

JavaScript (built-in):

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

// Serialize JavaScript object โ†’ JSON string
const json = JSON.stringify({ name: "Alice", age: 30 });
// '{"name":"Alice","age":30}'

// Pretty-print with 2-space indentation
const pretty = JSON.stringify({ name: "Alice" }, null, 2);

Python:

import json

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

# Serialize
json_string = json.dumps({"name": "Alice", "age": 30}, indent=2)

Common JSON Pitfalls

1. Trailing commas โ€” The most common mistake. Unlike JavaScript objects, JSON does not allow trailing commas:

// INVALID JSON
{"name": "Alice", "age": 30,}

// VALID JSON
{"name": "Alice", "age": 30}

2. Comments are not allowed โ€” JSON has no comment syntax. Use separate documentation or wrapper formats like JSON5 or JSONC if you need comments.

3. Single quotes โ€” JSON requires double quotes. {'name': 'Alice'} is not valid JSON.

4. Undefined and functions โ€” JavaScript has undefined and functions, but these cannot be represented in JSON. JSON.stringify silently drops them.

5. Large numbers โ€” JavaScript uses 64-bit floating point, which can't represent integers above 2โตยณ precisely. For large IDs, use strings instead.

6. Circular references โ€” JSON.stringify throws an error on objects with circular references.

JSON Use Cases

  • REST APIs โ€” Request and response bodies
  • Configuration files โ€” package.json, tsconfig.json, etc.
  • NoSQL databases โ€” MongoDB, Firestore, DynamoDB all use JSON-like document storage
  • Local storage โ€” Browser localStorage stores strings; serialize objects with JSON.stringify
  • Message queues โ€” Event payloads in message systems

Use our free JSON Formatter to pretty-print, validate, and minify JSON. We also offer JSON to YAML and JSON to XML converters.

Share this article