JSON Object Tutorial
Last updated:
A JSON object is an unordered collection of string-keyed value pairs enclosed in curly braces {} — one of the 2 structural types in JSON (the other is array). Keys must be double-quoted strings; values can be any of the 6 JSON types: string, number, boolean, null, array, or another object. Unlike JavaScript objects, JSON objects cannot have numeric keys, Symbol keys, or prototype methods. The JSON specification does not guarantee key order, though most parsers preserve insertion order in practice. This guide covers object syntax, nested objects, key naming conventions, value access, merging, and the difference between a JSON object and a JavaScript object.
Format and validate JSON objects online
Paste any JSON object to beautify, validate syntax, and explore the structure in a collapsible tree.
Open JSON ViewerBasic syntax
Each key must be a double-quoted string, followed by a colon and a value:
{
"name": "Alice",
"age": 30,
"active": true
}Rules:
- Keys must be double-quoted strings
- Key-value pairs are separated by commas
- No trailing comma after the last pair
- Values can be strings, numbers, booleans, null, arrays, or other objects
All value types in a JSON object
{
"string": "hello",
"integer": 42,
"float": 3.14,
"boolean": true,
"null": null,
"array": [1, 2, 3],
"object": { "nested": "value" }
}Nested JSON objects
Objects can be nested at any depth. Each nested object follows the same rules as the top-level object.
{
"company": {
"name": "Acme Corp",
"address": {
"street": "123 Main St",
"city": "Springfield",
"country": "US"
},
"employees": 150
}
}Empty object
An empty object is valid JSON:
{}Often used as a default or placeholder value in APIs.
Access object properties in JavaScript
const json = '{"name": "Alice", "age": 30, "address": {"city": "NYC"}}';
const data = JSON.parse(json);
// Dot notation
console.log(data.name); // "Alice"
console.log(data.address.city); // "NYC"
// Bracket notation (required for dynamic keys or special chars)
console.log(data["age"]); // 30
// Optional chaining (safe for nested access)
console.log(data?.address?.zip); // undefined (no error if missing)
// Destructuring
const { name, age } = data;
console.log(name, age); // Alice 30
// Check if a key exists
console.log("name" in data); // true
console.log(data.hasOwnProperty("email")); // falseIterate over object keys in JavaScript
const data = { name: "Alice", age: 30, active: true };
// Object.keys() — array of keys
Object.keys(data).forEach(key => {
console.log(key, data[key]);
});
// Object.entries() — array of [key, value] pairs
for (const [key, value] of Object.entries(data)) {
console.log(`${key}: ${value}`);
}
// Object.values() — array of values
const values = Object.values(data);
console.log(values); // ["Alice", 30, true]Access object properties in Python
import json
json_string = '{"name": "Alice", "age": 30, "address": {"city": "NYC"}}'
data = json.loads(json_string)
# Direct access
print(data["name"]) # Alice
# Safe access with .get()
print(data.get("email", "not set")) # not set
# Nested access
print(data["address"]["city"]) # NYC
# Check key existence
print("name" in data) # TrueIterate over a JSON object in Python
import json
data = json.loads('{"name": "Alice", "age": 30, "active": true}')
# Iterate keys
for key in data:
print(key, data[key])
# Iterate key-value pairs
for key, value in data.items():
print(f"{key}: {value}")
# Get all keys and values
keys = list(data.keys())
values = list(data.values())Merge JSON objects in JavaScript
const defaults = { theme: "dark", language: "en", timeout: 30 };
const userPrefs = { theme: "light", fontSize: 14 };
// Spread operator (userPrefs overrides defaults)
const config = { ...defaults, ...userPrefs };
console.log(config);
// { theme: "light", language: "en", timeout: 30, fontSize: 14 }
// Object.assign() — same result
const config2 = Object.assign({}, defaults, userPrefs);Common mistakes
- Single-quoted keys —
{"key": "val"}is valid;{"{'key': 'val'}"}is not valid JSON. - Unquoted keys —
{key: "val"}is JavaScript object literal syntax, not JSON. - Trailing comma —
{"a": 1, "b": 2,}is invalid. - Duplicate keys — JSON allows duplicate keys but behavior is undefined. Most parsers keep the last value.
Format and validate JSON objects online
Use the JSON Formatter to format and validate any JSON object, or the tree view to explore nested structures visually. For enforcing a specific object structure, use the JSON Schema Validator.
Frequently asked questions
What is a JSON object?
A JSON object is an unordered collection of key-value pairs enclosed in curly braces. Keys must be double-quoted strings. Values can be any JSON type. Key-value pairs are comma-separated with no trailing comma. JSON objects are parsed to JavaScript objects or Python dictionaries.
Are JSON object keys case-sensitive?
Yes. "name", "Name", and "NAME" are three different keys. JSON Schema validation is also case-sensitive. Choose a consistent casing convention (camelCase is standard for JSON APIs) and document it clearly.
Can JSON object keys be numbers?
No. JSON keys must be double-quoted strings. {1: "one"} is invalid JSON. If you need numeric-looking keys, write them as strings: {"1": "one"}. Consider using an array if you need ordered numeric indexing.
What is the difference between a JSON object and a JavaScript object?
JSON is a text format with strict rules (double-quoted keys, no functions, no comments). A JavaScript object is an in-memory data structure that allows unquoted keys, functions, Date objects, and more. JSON.stringify() converts JS to JSON text; JSON.parse() converts back.
How do I access a JSON object property?
After JSON.parse(): use dot notation (data.name) or bracket notation (data["name"]). Chain for nested access: data.address.city. Use optional chaining (data?.address?.zip) to safely access potentially missing properties.
Can JSON objects have duplicate keys?
The spec allows it but calls behavior undefined. Most parsers (JavaScript, Python) keep the last value. Duplicate keys should be avoided — some strict parsers reject them, and behavior is inconsistent across environments.
Recommended reading
- Designing Data-Intensive Applications (2nd Edition) — Martin Kleppmann & Chris RiccominiThe modern classic on data systems — encoding formats, schemas, replication, and stream processing.
- JavaScript: The Definitive Guide (7th Edition) — David FlanaganThe complete reference for the language JSON came from — serialization, async, and the full standard library.
As an Amazon Associate, Jsonic earns from qualifying purchases.