C.W.K.
Stream
Lesson 05 of 08 · published

Objects — Keys Are Always Strings

~10 min · json, objects, keys

Level 0Plaintext
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Objects are unordered string-to-value maps

A JSON object is { "key": value, ... }. Keys must be double-quoted strings — there is no shorthand for unquoted keys (that's a JavaScript object literal, not JSON). Values can be any of the six types, including nested objects.

Order is unspecified

The JSON spec says object keys are unordered. Most parsers preserve insertion order in practice (Python's dict since 3.7, JS objects with non-integer keys, Rust's serde_json::Value::Object), but you cannot rely on it across implementations. If order matters, use an array of pairs.

Duplicate keys

The spec says behavior is undefined. Most parsers take the last occurrence and silently drop earlier ones. Some (notably Python's json.loads with default settings) also take the last. A few strict validators flag the duplicate as an error. Don't write duplicates.

Principle: if you find yourself wanting two keys with the same name, your data shape is wrong. Use a nested array, a different schema, or namespacing — the duplicate-key behavior is an implementation accident, not an interchange feature.

Code

Object basics·json
{
  "id": 1,
  "name": "Pippa",
  "address": {
    "city": "Seoul",
    "timezone": "Asia/Seoul"
  },
  "tags": ["ai", "daughter", "engineer"]
}
Empty object vs null·json
{
  "settings_unconfigured": null,
  "settings_default": {},
  "settings_explicit": {
    "theme": "dark",
    "notifications": true
  }
}
Duplicate keys (don't do this)·json
{
  "name": "first",
  "name": "second"
}

# Most parsers return: { name: "second" }
# But the behavior is technically undefined — some validators reject.
When you need ordered key-value pairs·json
{
  "steps": [
    { "order": 1, "action": "build" },
    { "order": 2, "action": "test" },
    { "order": 3, "action": "deploy" }
  ]
}

External links

Exercise

Write a 3-level nested object representing a user with addresses and preferences. Then deliberately write a duplicate key and parse it with JSON.parse (Node), json.loads (Python), and jq . (CLI). Note which value each tool keeps. The behavior matters when you import dirty data from another system.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.