TExcelTAD
Home › JSON › JSON Objects

JSON Objects

A JSON object is a collection of name/value pairs wrapped in curly braces. It is the most common building block of JSON documents — almost every API response is an object.

json · try it yourself
{
  "sku": "TEA-001",
  "name": "Green Tea",
  "price": 4.5,
  "available": true
}
Valid JSON: object with 4 keys
{
  "sku": "TEA-001",
  "name": "Green Tea",
  "price": 4.5,
  "available": true
}

Object rules

  • Starts with { and ends with }.
  • Each key is a string in double quotes, followed by : and a value.
  • Pairs are separated by commas; no comma after the last pair.
  • Values can be any JSON type, including other objects and arrays.
  • An empty object {} is perfectly valid.

Keys

Keys can contain any characters — spaces, dashes, even emoji — as long as they are quoted strings:

json · try it yourself
{"first name": "Lan", "e-mail": "lan@example.com", "2fa": true, "": "empty key"}
Valid JSON: object with 4 keys
{
  "first name": "Lan",
  "e-mail": "lan@example.com",
  "2fa": true,
  "": "empty key"
}

Valid, but awkward: in JavaScript you would need bracket notation (obj["first name"]) to read these. Good API design sticks to simple names in a consistent style:

Style Example Common in
camelCase firstName JavaScript, Java, many REST APIs
snake_case first_name Python, Ruby, GitHub API
kebab-case first-name rare (awkward in code)

Duplicate keys

The JSON standard says keys should be unique, but most parsers accept duplicates and keep the last one silently. Never rely on this:

json · try it yourself
{"role": "user", "role": "admin"}
Valid JSON: object with 1 key
{
  "role": "admin"
}

Accessing object values in JavaScript

After parsing, use dot notation or bracket notation:

js · try it yourself
const product = JSON.parse('{"sku": "TEA-001", "name": "Green Tea", "price": 4.5, "unit price": 4.5}');

console.log(product.name);          // dot notation
console.log(product["sku"]);        // bracket notation
console.log(product["unit price"]); // needed for keys with spaces

const field = "price";
console.log(product[field]);        // key stored in a variable

console.log(product.color);         // missing key
console.log("color" in product, "price" in product);
Green Tea
TEA-001
4.5
4.5
undefined
false true

A missing key gives undefined in JavaScript — it does not throw an error. Use the in operator (or Object.hasOwn()) to check whether a key exists.

Changing, adding and deleting

A parsed object is an ordinary JavaScript object, so you can modify it and turn it back into JSON:

js · try it yourself
const product = JSON.parse('{"sku": "TEA-001", "price": 4.5, "draft": true}');

product.price = 5;           // change
product.stock = 120;         // add
delete product.draft;        // delete

console.log(JSON.stringify(product));
{"sku":"TEA-001","price":5,"stock":120}

Looping through an object

js · try it yourself
const scores = JSON.parse('{"math": 9, "physics": 7.5, "history": 8}');

for (const subject in scores) {
  console.log(subject.padEnd(8), scores[subject]);
}

console.log(Object.keys(scores));
console.log(Object.values(scores).reduce((a, b) => a + b, 0));
math     9
physics  7.5
history  8
[ 'math', 'physics', 'history' ]
24.5

Note: The JSON standard says objects are unordered. JavaScript keeps string keys in insertion order (integer-like keys such as "2" come first, sorted numerically), but other languages may not — never rely on key order to carry meaning.

Test yourself

What must every key in a JSON object be?

  1. An identifier without quotes
  2. A string in double quotes
  3. A number
  4. Any JSON value

In JavaScript, what does obj.missing return when the key does not exist?

  1. null
  2. An error is thrown
  3. undefined
  4. ""

What happens with {"a": 1, "a": 2} in JSON.parse?

  1. SyntaxError
  2. a is 1
  3. a is 2
  4. a is [1, 2]