JSON (JavaScript Object Notation) is a lightweight, text-based format for storing and exchanging data. Despite having "JavaScript" in its name, JSON is language-independent and supported natively by virtually every modern programming language.
The Basic Structure
A JSON document is built from two fundamental structures:
- Objects - An unordered collection of key-value pairs, enclosed in curly braces
{} - Arrays - An ordered list of values, enclosed in square brackets
[]
Here is a simple JSON object representing a user:
{
"name": "Alice",
"age": 30,
"isActive": true,
"address": {
"city": "New York",
"zip": "10001"
},
"tags": ["developer", "designer"]
}
JSON Data Types
JSON supports exactly six data types:
- String - Text wrapped in double quotes:
"hello" - Number - Integer or decimal:
42,3.14 - Boolean -
trueorfalse - Null - Represents no value:
null - Object - A nested set of key-value pairs:
{...} - Array - An ordered list:
[...]
Note that JSON does not support undefined, dates (stored as strings), or functions.
JSON Syntax Rules
JSON has a small set of strict rules:
- All keys must be strings enclosed in double quotes
- String values must also use double quotes (not single quotes)
- No trailing commas are allowed after the last item
- No comments are allowed inside JSON
Why JSON Replaced XML
Before JSON, XML was the dominant data exchange format for web services. JSON replaced it in most use cases because it is:
- Simpler to read and write - far less verbose than XML
- Easier to parse - built into JavaScript natively via
JSON.parse()andJSON.stringify() - Smaller payloads - reduces bandwidth usage
- Universally supported - every modern language has a JSON library
Where You Will Find JSON
JSON is used throughout modern software development:
- REST APIs - the standard format for API responses and request bodies
- Configuration files -
package.jsonin Node.js,settings.jsonin VS Code - Databases - MongoDB, Firestore, and PostgreSQL's
JSONBcolumn type - Browser storage -
localStorageandsessionStoragestore JSON strings - Log files - structured logging commonly uses JSON for machine-readable output
Parsing JSON in Code
In JavaScript, parsing a JSON string is a single function call:
const jsonString = '{"name": "Alice", "age": 30}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "Alice"
// Convert an object back to a JSON string
const str = JSON.stringify(obj);
console.log(str); // '{"name":"Alice","age":30}'
In Python it is equally straightforward:
import json
json_string = '{"name": "Alice", "age": 30}'
obj = json.loads(json_string)
print(obj["name"]) # Alice
# Convert back to string
print(json.dumps(obj)) # {"name": "Alice", "age": 30}
Explore Your JSON Structure
Paste any JSON into JSON Keyper and instantly extract every key path - including nested objects and arrays. Free, no login required.
Open JSON Keyper