TOML Tables and JSON Objects
Understand how TOML tables map to JSON objects. Learn about standard tables, dotted keys, and how TOML's section-based structure translates to nested JSON.
Detailed Explanation
Tables are TOML's primary structuring mechanism, equivalent to JSON objects. Understanding tables is essential for working with any non-trivial TOML document.
TOML tables with a header:
[owner]
name = "Tom Preston-Werner"
organization = "GitHub"
[database]
server = "192.168.1.1"
ports = [8001, 8001, 8002]
enabled = true
Converts to JSON:
{
"owner": {
"name": "Tom Preston-Werner",
"organization": "GitHub"
},
"database": {
"server": "192.168.1.1",
"ports": [8001, 8001, 8002],
"enabled": true
}
}
Table header syntax:
[tablename]creates a new object in the output- All key-value pairs below a header belong to that table
- A new header starts a new table
Dotted keys for inline nesting:
fruit.apple.color = "red"
fruit.apple.taste = "sweet"
fruit.orange.color = "orange"
Produces:
{
"fruit": {
"apple": { "color": "red", "taste": "sweet" },
"orange": { "color": "orange" }
}
}
Dotted table headers:
[servers.alpha]
ip = "10.0.0.1"
role = "frontend"
[servers.beta]
ip = "10.0.0.2"
role = "backend"
This creates a nested JSON structure with servers containing alpha and beta objects.
Key rules:
- Tables cannot be defined more than once (except with
[[array]]syntax). - Dotted keys and table headers can be combined, but a key cannot be redefined.
- The ordering of tables in TOML is preserved but not semantically meaningful in JSON (JSON objects are unordered by spec).
Use Case
Structuring application configuration with logical sections like [server], [database], and [logging] in TOML, and understanding how these sections map to nested JSON objects when consumed by your application's config parser.