Lesson 5 — Structure

Arrays in JSON

JSON arrays hold ordered lists of values, which can be simple types or complex nested objects, making them ideal for representing collections.

Arrays of Values and Objects

An array can hold a simple list of strings or numbers, or a list of full objects — the latter is extremely common in API responses representing collections, like a list of products or users.

JSON — An array of objects
{
  "products": [
    { "id": 1, "name": "Notebook", "price": 45.99 },
    { "id": 2, "name": "Pen", "price": 9.99 },
    { "id": 3, "name": "Backpack", "price": 399.00 }
  ]
}

Common Mistakes

  • Forgetting that array elements are accessed by numeric position (starting at 0) when consumed by code, not by name.
  • Mixing unrelated data types within a single array (a string, then a number, then an object) without a clear reason, making the array harder to process predictably.
  • Adding a trailing comma after the final element in an array.
  • Confusing an array of objects with a single object that happens to have numeric-looking keys — these are structurally very different.

Professional Tip

When an API might return zero, one, or many of something, model it as an array even if you expect only one result most of the time. This keeps the structure consistent regardless of how many items are actually present.

Your Turn

Write a JSON structure representing a shopping cart with an array of item objects, each having a product name, quantity, and unit price.

Mini Quiz

What is the index of the first element in a JSON array, once parsed into most programming languages?