What is JSON? Beginner's Guide with Syntax, Examples
JSON (JavaScript Object Notation) is a text format for data exchange — keys, values, arrays, objects. Universal in APIs, configs, NoSQL databases.
What is JSON?
JSON (JavaScript Object Notation) is a lightweight, text-based data format for representing structured data. Despite the name, it's language-agnostic — every modern programming language has libraries to parse and generate it. JSON is the dominant format for web APIs (REST, GraphQL responses), configuration files, and NoSQL databases.
JSON was popularized in the early 2000s as a simpler alternative to XML. Its appeal: human-readable, less verbose than XML, native to JavaScript, and easy to parse in any language.
JSON syntax basics
JSON has six data types:
- Object: collection of key-value pairs in
{} - Array: ordered list in
[] - String: text in
"double quotes" - Number: integer or float (no quotes)
- Boolean:
trueorfalse - null: explicit absence of value
JSON example
{
"id": 42,
"name": "Alice Johnson",
"email": "alice@example.com",
"active": true,
"login_count": 1247,
"last_login": "2026-05-04T14:30:00Z",
"preferences": {
"language": "en",
"timezone": "Europe/Berlin",
"notifications": false
},
"tags": ["admin", "beta-tester"],
"profile_image": null
}JSON syntax rules
- Keys must be strings, in double quotes
- Strings must use double quotes (not single quotes)
- No trailing commas (legal in JS, illegal in JSON)
- No comments (illegal in standard JSON; JSON5 allows them)
- No undefined; use null
- Numbers can't have leading zeros (except 0.x)
- Special chars in strings must be escaped:
\",\\,\n,\t
Common JSON operations
Parse JSON (string → object)
// JavaScript
const data = JSON.parse('{"id": 42}');
console.log(data.id); // 42
// Python
import json
data = json.loads('{"id": 42}')
print(data['id']) # 42
// Go
var data map[string]interface{}
json.Unmarshal([]byte(`{"id": 42}`), &data)Stringify JSON (object → string)
// JavaScript
const json = JSON.stringify({ id: 42, name: 'Alice' });
// Python
import json
json_str = json.dumps({'id': 42, 'name': 'Alice'})JSON vs XML vs YAML
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Verbosity | Low | High | Lowest |
| Comments | No (standard) | Yes | Yes |
| Schema validation | JSON Schema | XSD | JSON Schema |
| Best for | APIs, config | Document data | Configs, k8s |
| Browser support | Native | Native | Library required |
JSON in REST APIs
Modern REST APIs use JSON for request and response bodies:
# Request
POST /api/users HTTP/1.1
Content-Type: application/json
{ "email": "alice@example.com", "name": "Alice" }
# Response
HTTP/1.1 201 Created
Content-Type: application/json
{ "id": 42, "email": "alice@example.com", "name": "Alice" }Always set Content-Type: application/json on requests with JSON bodies.
JSON Schema: validating JSON
JSON Schema is a vocabulary for describing JSON data structures. Used for validation, documentation, code generation:
{
"type": "object",
"required": ["email", "name"],
"properties": {
"email": { "type": "string", "format": "email" },
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 }
}
}OpenAPI specs use JSON Schema for request/response shapes.
JSON best practices
- Use ISO 8601 for dates.
"2026-05-04T14:30:00Z"— sortable, unambiguous. - Consistent naming. Pick camelCase or snake_case; don't mix.
- Validate against a schema. JSON Schema in CI catches malformed data early.
- Compress with gzip / Brotli. JSON's repeated keys compress well (often 80%+).
- Use null over missing keys for optional fields. Easier to consume.
- Keep numbers within JS safe range. 53-bit integer max (
Number.MAX_SAFE_INTEGER); use string for bigger IDs. - Don't put binary in JSON. Base64-encode if you must, or use a separate URL.
- Stream large arrays. Don't load 1GB JSON into memory; use streaming parsers (Jackson, ijson).
Common JSON pitfalls
- Single quotes. Invalid JSON; many tools silently fail.
- Trailing commas. Not allowed; use a linter.
- Comments in standard JSON. Illegal; use JSON5/JSONC for configs that need comments.
- Number precision. Sending
9007199254740993as a number gets corrupted by JS's float64. Use string for big IDs. - Undefined dates. Pick a date format and stick to it (ISO 8601).
- Inconsistent shapes. Same endpoint returning different schemas; use schema validation.
- HTML escaping in JSON. Some templating engines double-escape; corrupt the JSON.
- JSON injection. Build JSON from string concat = SQL-injection-style risk; always use library serializers.
Variants of JSON
| Variant | Adds | Use case |
|---|---|---|
| JSON5 | Comments, trailing commas, single quotes | Config files |
| JSONC | Comments only | VS Code settings |
| NDJSON | Newline-delimited records | Log streaming, big data |
| JSON-LD | Linked data semantics | SEO structured data, schema.org |
| GeoJSON | Geographic shapes | Maps, GIS |
| JSON Patch | Diff format | API partial updates |
FAQ: JSON
Why JSON over XML?
Smaller, simpler syntax, native JS parsing, easier to write by hand. XML still wins for document-style data with attributes + namespaces.
Can JSON have comments?
Standard JSON: no. JSON5 / JSONC: yes. For configs needing comments, use those variants.
How do I send dates in JSON?
ISO 8601 strings: "2026-05-04T14:30:00Z". Sortable lexicographically; unambiguous.
What about big numbers?
JavaScript's max safe integer is 2^53 - 1. For larger IDs (e.g., 64-bit), serialize as string to avoid precision loss.
Is JSON safe to parse from untrusted sources?
JSON.parse is safe (no code execution). But validate the shape via schema before using the data — don't trust untrusted JSON to match your expected structure.
What's the difference between JSON and JavaScript objects?
JSON is a text format. JS objects are runtime data. JSON has stricter rules (double quotes only, no functions, no undefined, no comments).
How do I pretty-print JSON?
JSON.stringify(obj, null, 2) in JS; json.dumps(obj, indent=2) in Python; jq . in shell.
Test JSON APIs at scale with LoadFocus
LoadFocus runs JMeter and k6 scripts that hit JSON APIs from 25+ regions, validating schema, performance, and concurrency. Sign up free at loadfocus.com/signup.
Related LoadFocus Tools
Put this concept into practice with LoadFocus — the same platform that powers everything you just read about.