What is JSON?

Last updated:

JSON (JavaScript Object Notation) is a text-based data interchange format defined in RFC 8259 (published 2017) and ECMA-404, encoding structured data as plain UTF-8 text using exactly 6 types: string, number, boolean, null, array, and object. JSON replaced XML as the dominant API payload format — JSON responses are typically 30–50% smaller than equivalent XML for the same data, and parsers exist in every major language. Despite the name, JSON is language-independent: Python, Java, Go, Rust, Ruby, and PHP all have built-in parsers; files use the .json extension and MIME type application/json. This guide covers JSON syntax rules, all 6 data types, valid vs invalid values, parsing with JSON.parse(), serializing with JSON.stringify(), and common pitfalls like trailing commas and comments.

Try JSON online

Paste any JSON to validate, format, and explore it instantly — no account required.

Open JSON Formatter

JSON definition

Despite the name "JavaScript Object Notation," JSON is language-independent: parsers are built into Python, Java, Go, Rust, Ruby, PHP, and every other major runtime. Files use the .json extension and the MIME type application/json. A JSON document is a single value — most commonly an object or array — encoded as plain UTF-8 text that can be stored in a file, transmitted over HTTP, or embedded in another document.

JSON replaced XML as the standard API payload format because it maps directly to native data structures in most languages and produces responses that are typically 30–50% smaller than equivalent XML for the same data.

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.

Frequently asked questions

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and exchanging data. It represents data as key-value pairs and ordered lists, using syntax derived from JavaScript object literals. JSON is language-independent — parsers exist for every major programming language — and was standardized in RFC 8259 and ECMA-404.

What is JSON used for?

JSON is the dominant format for web APIs, configuration files, and data storage. REST APIs use JSON for request and response bodies. Config files like package.json and tsconfig.json are JSON. NoSQL databases like MongoDB store documents in a JSON-like format. Browsers persist JSON strings in localStorage for client-side state.

What are the JSON data types?

JSON supports exactly 6 data types: string (double-quoted text), number (integer or decimal), boolean (true or false), null, object (key-value pairs in curly braces), and array (ordered list in square brackets). undefined, NaN, Infinity, functions, and dates are not valid JSON values.

Is JSON the same as a JavaScript object?

No. JSON is a text format; a JavaScript object is a runtime data structure. JSON requires double quotes on all keys and string values, prohibits trailing commas and comments, and does not support functions, undefined, or NaN. Use JSON.parse() to convert a JSON string to a JavaScript object, and JSON.stringify() to go the other direction.

What is the difference between JSON and XML?

JSON is more compact and maps directly to native data structures in most languages. XML supports attributes, mixed content, comments, and XSD schemas. JSON has replaced XML in most REST API contexts — a typical JSON response is 30–50% smaller than the equivalent XML for the same data, and it requires no parsing library beyond what every language runtime already includes.

Does JSON support comments?

No. Standard JSON (RFC 8259) does not allow comments. If you need comments in a JSON-like format, use JSON5 (supports // and /* */ comments) or JSONC, which VS Code uses for tsconfig.json and settings files.

Further reading and primary sources