What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and exchanging data. It is the dominant format for web APIs and configuration files — readable by humans and easily parsed by machines in any programming language.

JSON definition

JSON is a data format derived from JavaScript object literal syntax. Despite the name, JSON is language-independent: parsers exist for Python, Java, Go, Rust, Ruby, PHP, and every other major language. It was standardized in RFC 8259 and ECMA-404.

A JSON document is a single value — most commonly an object or array — encoded as plain UTF-8 text. That text can be stored in a file, transmitted over HTTP, or embedded in another document.

JSON data types

JSON supports exactly six data types:

TypeExampleNotes
string"hello"Must use double quotes
number42, 3.14, -7Integer or decimal, no quotes
booleantrue, falseLowercase only
nullnullRepresents absence of value
object{"key": "value"}Unordered key-value pairs
array[1, 2, 3]Ordered list of values

Notably absent: undefined, NaN, Infinity, functions, dates (use ISO 8601 strings), and comments.

A complete JSON example

{
  "name": "Alice",
  "age": 30,
  "active": true,
  "score": null,
  "tags": ["admin", "user"],
  "address": {
    "city": "London",
    "country": "UK"
  }
}

This object has six keys demonstrating all JSON types: a string, a number, a boolean, null, an array, and a nested object.

JSON syntax rules

  • Strings must be enclosed in double quotes — single quotes are not valid.
  • Object keys must be strings: {"key": "value"}, not {key: "value"}.
  • The last item in an object or array must not have a trailing comma.
  • Comments are not allowed.
  • Numbers must not have leading zeros (07 is invalid).
  • Special values like undefined, NaN, and Infinity are not valid JSON.

JSON vs JavaScript objects

JSON looks almost identical to a JavaScript object literal, but they are different things:

FeatureJSONJavaScript object
Key quotesRequired (double quotes)Optional (any quote or none)
String quotesDouble quotes onlySingle, double, or template
Trailing commasNot allowedAllowed
CommentsNot allowedAllowed
FunctionsNot allowedAllowed
undefinedNot allowedAllowed

Parsing JSON in JavaScript

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

// Convert an object to a JSON string
const person = { name: "Bob", age: 25 };
const jsonString = JSON.stringify(person);
// '{"name":"Bob","age":25}'

// Pretty-print JSON
const pretty = JSON.stringify(person, null, 2);
// '{
//   "name": "Bob",
//   "age": 25
// }'

Parsing JSON in Python

import json

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

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

# Serialize to JSON string
person = {"name": "Bob", "age": 25}
json_str = json.dumps(person, indent=2)

What is JSON used for?

  • Web APIs: The vast majority of REST APIs send and receive JSON. When you call fetch() in JavaScript or requests.get() in Python, the response body is typically JSON.
  • Configuration files: package.json, tsconfig.json, .eslintrc.json, VS Code settings — all JSON.
  • Data storage: NoSQL databases like MongoDB and Firestore store documents in JSON-like formats (BSON and Firestore documents).
  • localStorage: Browsers persist JSON strings in localStorage for client-side state.
  • Log formats: Structured logging tools emit newline-delimited JSON (NDJSON) for machine-readable log processing.

Validate and format JSON online

Paste any JSON into the JSON Formatter to instantly validate its syntax and pretty-print it with proper indentation. Common errors like trailing commas and unquoted keys are detected and reported with exact line numbers.