What is JSON?

A complete guide to JavaScript Object Notation

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. Despite its name containing "JavaScript," JSON is language-independent and is used by virtually all modern programming languages.

JSON was derived from JavaScript but has become the de facto standard for data exchange on the web. It's used extensively in web APIs, configuration files, data storage, and communication between servers and clients.

JSON Syntax Basics

JSON is built on two structures:

  • Objects: A collection of key/value pairs enclosed in curly braces { }
  • Arrays: An ordered list of values enclosed in square brackets [ ]

JSON Data Types

JSON supports the following data types:

String

"Hello World"

Number

42, 3.14, -17

Boolean

true, false

Null

null

Object

{"key": "value"}

Array

[1, 2, 3]

JSON Object Example

Here's an example of a JSON object representing a person:

{
  "firstName": "John",
  "lastName": "Doe",
  "age": 30,
  "email": "john.doe@example.com",
  "isActive": true,
  "address": {
    "street": "123 Main St",
    "city": "New York",
    "zipCode": "10001"
  },
  "phoneNumbers": [
    "+1-555-1234",
    "+1-555-5678"
  ],
  "spouse": null
}

JSON Array Example

Arrays in JSON can contain any valid JSON data type:

{
  "fruits": ["apple", "banana", "orange"],
  "numbers": [1, 2, 3, 4, 5],
  "mixed": [1, "two", true, null],
  "users": [
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30}
  ]
}

JSON Rules and Best Practices

✓ Keys must be strings in double quotes

Correct: {"name": "John"}
Wrong: {name: "John"}

✓ Strings must use double quotes

Correct: "Hello World"
Wrong: 'Hello World'

✓ No trailing commas

Correct: {"a": 1, "b": 2}
Wrong: {"a": 1, "b": 2,}

✓ No comments allowed

JSON does not support comments. Remove all // or /* */ comments from your JSON.

Common Uses of JSON

  • Web APIs: RESTful APIs commonly use JSON to send and receive data
  • Configuration Files: Many applications use JSON for configuration (package.json, tsconfig.json)
  • Data Storage: NoSQL databases like MongoDB store data in JSON-like formats
  • Data Exchange: Transferring data between a server and web application
  • Logging: Structured logging in JSON format for easier parsing and analysis

JSON vs XML

JSON has largely replaced XML for web data interchange. Here's why:

FeatureJSONXML
ReadabilityMore conciseMore verbose
Parsing SpeedFasterSlower
Data TypesBuilt-in typesAll strings
File SizeSmallerLarger

Ready to format your JSON?

Open JSON Formatter