TOML Array of Tables ([[table]]) and JSON Arrays of Objects
Learn how TOML's double-bracket [[table]] syntax creates arrays of objects in JSON. Understand this powerful feature for defining lists of structured configuration entries.
Detailed Explanation
The array of tables syntax ([[table]]) is one of TOML's most distinctive features. It provides a clean way to define arrays of objects that would otherwise require verbose inline table syntax.
TOML array of tables:
[[products]]
name = "Hammer"
sku = 738594937
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
Converts to JSON:
{
"products": [
{
"name": "Hammer",
"sku": 738594937
},
{
"name": "Nail",
"sku": 284758393,
"color": "gray"
}
]
}
Each [[products]] creates a new element in the products array. All key-value pairs between two [[products]] headers belong to that array element.
Nested arrays of tables:
[[fruits]]
name = "apple"
[[fruits.varieties]]
name = "red delicious"
[[fruits.varieties]]
name = "granny smith"
[[fruits]]
name = "banana"
[[fruits.varieties]]
name = "plantain"
Converts to JSON:
{
"fruits": [
{
"name": "apple",
"varieties": [
{"name": "red delicious"},
{"name": "granny smith"}
]
},
{
"name": "banana",
"varieties": [
{"name": "plantain"}
]
}
]
}
How it works:
[[fruits]]appends a new object to thefruitsarray.[[fruits.varieties]]appends a new object to thevarietiesarray inside the most recently definedfruitselement.- The next
[[fruits]]starts a fresh fruit object with its ownvarietiesarray.
When converting JSON to TOML:
Any JSON array where every element is an object should be converted to TOML's [[array]] syntax. This is more readable than inline tables for objects with multiple keys.
Comparison with inline tables:
# Inline (compact but hard to read for complex objects)
products = [{name = "Hammer", sku = 738594937}, {name = "Nail", sku = 284758393}]
# Array of tables (readable for complex objects)
[[products]]
name = "Hammer"
sku = 738594937
Use inline tables for simple objects with 1-2 keys, and array of tables for anything more complex.
Use Case
Defining multiple server endpoints, database replicas, or plugin configurations in a TOML file where each entry has several properties, and converting to JSON for programmatic consumption.