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

Arrays of Tables — Repeating Structured Items

~10 min · toml, arrays-of-tables

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

The [[name]] double-bracket form

Arrays of tables solve a real config-file pain: 'a list of structured things, each with multiple fields, that I want to read or edit one at a time.' The syntax is double brackets: [[products]]. Every [[products]] block in the file appends one element to the products array.

When to reach for it

  • An array of items, each with multiple fields.
  • Items you want to comment, edit, or grep individually.
  • Where inline-table arrays would be too long to read.

The [[]] vs [] distinction

[server] opens the server table (singular). [[plugins]] appends one element to the plugins array. Each [[plugins]] is its own self-contained block; the order in source = the order in the array.

Principle: when in doubt, structure that reads as 'one of N things' should be an array of tables, not a table of tables keyed by name. Arrays preserve order; reviewers can comment on each item; new entries append cleanly. Compare to YAML — same idea, less reliable diffs.

Code

Array of tables — three plugins·toml
[[plugins]]
name = "auth"
version = "1.2"
enabled = true

[[plugins]]
name = "rate-limit"
version = "0.4"
enabled = true
config = { window_sec = 60, max = 100 }

[[plugins]]
name = "experimental"
version = "0.1"
enabled = false
# Disabled until the perf regression lands
# (see #142)
Equivalent inline-table form (uglier for many fields)·toml
plugins = [
  { name = "auth",         version = "1.2", enabled = true },
  { name = "rate-limit",   version = "0.4", enabled = true,  config = { window_sec = 60, max = 100 } },
  { name = "experimental", version = "0.1", enabled = false },
]
# Use inline form for 2-3 short fields; use [[plugins]] when items are bigger.
Nested arrays of tables (Hugo menus pattern)·toml
[[menu.main]]
name = "Home"
url = "/"
weight = 1

[[menu.main]]
name = "Posts"
url = "/posts"
weight = 2

[[menu.main]]
name = "About"
url = "/about"
weight = 3

External links

Exercise

Convert a YAML list-of-objects (a list of CI jobs, a list of webhooks, a list of routes) into a TOML array of tables. Run the file through tomllib.load and confirm the parsed shape matches a list of dicts. Diff a 'add one new entry' commit in both forms — you should see TOML's noise-free diff win.

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.