TOML 02 📄 Syntax Fundamentals, Tables, and Key-Value Pairs
TOML’s syntax is built on a single primitive: the key-value pair. Everything else—tables, nested tables, arrays of tables—is a way of organizing those pairs into a structure that maps cleanly to a hash table. The format has no indentation sensitivity, no closing tags, and no implicit typing. A key is on the left of =, a value is on the right, and the line ends. The complexity lives in how keys and tables combine, not in the syntax of a single entry.
This chapter covers the building blocks: bare and quoted keys, dotted keys, standard tables, inline tables, and the root table that holds everything before the first header. It explains why tables cannot be redefined, how dotted keys create implicit tables, and the distinction between a table defined by a header and one defined by an inline braces expression. The sections that follow walk through key syntax, table headers, and the composition rules that determine how nested structures are built.
Key point: A TOML document is a hash table; key-value pairs fill the root table until a [table] header starts a new table, and every pair after that header belongs to that table until the next header.
Why tables and key rules exist
The structure problem. A flat list of key-value pairs cannot represent nested configuration. TOML solves this with tables: a [server] header means every pair until the next header belongs to a table named server. The header is not a value; it is a context switch. This is the same idea as INI sections, but with a specification that defines exactly how nesting and redefinition behave .
The redefinition problem. TOML forbids defining the same table twice. If [fruit] appears, then [fruit] appears again later, the parser must reject the document. The rule exists because a second definition would have no clear meaning: does it merge, replace, or error? TOML chooses error, which means the structure is determined entirely by the first occurrence of each table header . Dotted keys interact with this rule: fruit.apple.color = "red" creates fruit and fruit.apple implicitly, and those implicit tables cannot later be opened with a [fruit.apple] header .
The inline table problem. Standard tables are defined by headers and span multiple lines. An inline table { x = 1, y = 2 } defines a table in a single expression, fully self-contained. The trade-off is that an inline table cannot be extended later; its keys are fixed at the point of definition. Inline tables are useful for compact data like a point or a set of tags, while standard tables are for configuration sections that grow .
a. Keys: bare, quoted, and dotted
A key is the identifier on the left of =. Bare keys allow only ASCII letters, digits, underscores, and hyphens. Quoted keys allow any string, including characters that bare keys cannot contain. Dotted keys group related entries by creating nested tables .
# Bare keys: letters, digits, underscores, hyphens
name = "my-app"
port = 8080
max_retries = 3
# Quoted keys: any string
"key with spaces" = "value"
'unicode key ʞ' = "value"
# Dotted keys: create nested tables implicitly
fruit.apple.color = "red"
# Creates fruit, fruit.apple, and sets color inside fruit.apple
Whitespace around keys and around dots is ignored, but the best practice is to avoid extraneous whitespace: [a.b.c] is preferred over [ a . b . c ] . A bare key that is all digits is technically valid but discouraged because it can look like a float when combined with dots: 3.14159 = "pi" parses as { "3": { "14159": "pi" } }, not as a floating-point key .
b. Standard tables and the root table
A standard table begins with a header: a name enclosed in square brackets on a line by itself. Everything after the header, until the next header or end-of-file, belongs to that table .
# Root table: starts at the beginning of the document
title = "My Application"
# First table: everything after this header belongs to [server]
[server]
host = "0.0.0.0"
port = 8080
# Second table: [database] starts here
[database]
host = "db.example.com"
port = 5432
The root table is unnamed and exists before the first header. It ends when the first [table] header appears; after that, the root table cannot be reopened . A table can be nested by using a dotted name in the header: [database.credentials] defines a table named credentials inside database. TOML automatically creates the intermediate database table if it does not exist; there is no need to write [database] separately unless it has its own key-value pairs .
# No need to define [database] first
[database.credentials]
username = "app_user"
password = "secret"
A table cannot be defined more than once. The following is invalid because [fruit] appears twice :
# INVALID
[fruit]
apple = "red"
[fruit]
orange = "orange"
c. Inline tables
An inline table is written between braces on a single line: { key = value, key = value }. It defines a complete table at the point of use and cannot be extended afterward .
# Inline table for compact data
point = { x = 1, y = 2 }
# Equivalent standard table form
[point]
x = 1
y = 2
The inline form is useful when a table is small and belongs inside another structure, such as an array of inline tables. The standard form is preferable when the table has many keys or needs to be extended later. An inline table cannot be followed by a [point.extra] header to add keys; that would be a redefinition error .
Complete Example Session
# ============================================
# PART 1: ROOT TABLE (NO HEADER)
# ============================================
title = "My Service"
version = "1.0.0"
enabled = true
# ============================================
# PART 2: BARE AND QUOTED KEYS
# ============================================
name = "app"
"key with spaces" = "value"
'quoted "value"' = "also valid"
# ============================================
# PART 3: DOTTED KEYS CREATE TABLES
# ============================================
database.host = "localhost"
database.port = 5432
# Creates database table implicitly
# ============================================
# PART 4: STANDARD TABLE HEADER
# ============================================
[server]
host = "0.0.0.0"
port = 8080
# ============================================
# PART 5: NESTED TABLE HEADER
# ============================================
[server.tls]
enabled = true
cert = "/path/to/cert.pem"
# ============================================
# PART 6: INLINE TABLE
# ============================================
logging = { level = "info", format = "json" }
# ============================================
# PART 7: THE PARSED HASH TABLE
# ============================================
# {
# title: "My Service",
# version: "1.0.0",
# enabled: true,
# name: "app",
# "key with spaces": "value",
# 'quoted "value"': "also valid",
# database: { host: "localhost", port: 5432 },
# server: {
# host: "0.0.0.0",
# port: 8080,
# tls: { enabled: true, cert: "/path/to/cert.pem" }
# },
# logging: { level: "info", format: "json" }
# }
The parts covered the root table, bare and quoted keys, dotted keys creating implicit tables, a standard table header, a nested table header, an inline table, and the resulting hash table structure.
Quick Reference
Key Types
| Type | Syntax | Restrictions |
|---|---|---|
| Bare | name = "value" | ASCII letters, digits, _, - |
| Quoted | "key with spaces" = "value" | Any string |
| Dotted | fruit.apple.color = "red" | Creates implicit tables |
Table Forms
| Form | Syntax | Extendable |
|---|---|---|
| Standard | [table] header | Yes |
| Nested | [parent.child] header | Yes |
| Inline | { key = value } | No |
| Root | No header | Ends at first header |
Definition Rules
| Rule | Detail |
|---|---|
| Table redefinition | Invalid; each table defined once |
| Dotted key tables | Cannot later open with [table] header |
| Inline table extension | Not allowed after definition |
| Intermediate tables | Created automatically by dotted headers |
| Root table | Unnamed, cannot be reopened |
Best Practices
✅ Do This:
# No whitespace inside table headers
[a.b.c] # ✅
# Define dotted keys together for clarity
database.host = "localhost"
database.port = 5432 # ✅
# Use inline tables for small, fixed data
point = { x = 1, y = 2 } # ✅
# Use standard tables for config sections
[server]
host = "0.0.0.0" # ✅
❌ Don’t Do This:
# Redefine a table
[fruit]
apple = "red"
[fruit]
orange = "orange" # ❌
# Open a table created by dotted keys
fruit.apple.color = "red"
[fruit.apple] # ❌
# Extend an inline table
point = { x = 1 }
[point.extra] # ❌
# Extraneous whitespace in headers
[ a . b . c ] # ❌ (works but discouraged)
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Table redefinition error | Same [table] header twice | Combine into one section |
| Cannot add to dotted-key table | Implicit table already defined | Define all keys in one place |
| Inline table not extendable | Fully self-contained | Use standard table for growth |
| Numeric bare key parsed as float | 3.14159 with dots | Quote the key or avoid |
| Root table entries after header | Content after [table] belongs to table | Move root entries before first header |
Real-World Examples
1. Root table
title = "My App"
version = "1.0"
2. Standard table
[server]
port = 8080
3. Nested table header
[database.credentials]
username = "app"
4. Inline table
point = { x = 1, y = 2 }
5. Dotted keys
fruit.apple.color = "red"
6. Quoted key
"key with spaces" = "value"
7. Bare key with hyphen
max-retries = 3
8. Empty table
[empty]
9. Table with key-value pairs
[server]
host = "localhost"
port = 8080
10. Multiple tables
[server]
port = 8080
[database]
port = 5432
Visual
Root Table and Header Boundaries
┌─────────────────────────────────────────────────────────┐
│ DOCUMENT STRUCTURE │
│ │
│ title = "App" ◄── root table │
│ version = "1.0" ◄── root table │
│ │
│ [server] ◄── header: root ends │
│ host = "0.0.0.0" ◄── belongs to server │
│ port = 8080 ◄── belongs to server │
│ │
│ [database] ◄── header: server ends │
│ host = "db" ◄── belongs to database │
│ │
│ Each header switches context │
└─────────────────────────────────────────────────────────┘
Dotted Keys Create Implicit Tables
┌─────────────────────────────────────────────────────────┐
│ fruit.apple.color = "red" │
│ │
│ Creates: │
│ ┌──────────────────────────────────────────┐ │
│ │ fruit │ │
│ │ └── apple │ │
│ │ └── color = "red" │ │
│ └──────────────────────────────────────────┘ │
│ │
│ Cannot later write: │
│ [fruit.apple] ◄── ERROR (already defined) │
│ │
│ But can add: │
│ fruit.apple.taste = "sweet" ◄── new key is fine │
└─────────────────────────────────────────────────────────┘
Standard vs Inline Table
┌─────────────────────────────────────────────────────────┐
│ STANDARD TABLE INLINE TABLE │
│ │
│ [point] point = { x = 1, y = 2 } │
│ x = 1 │
│ y = 2 │
│ │
│ Multi-line Single line │
│ Extendable with headers Self-contained │
│ Best for config sections Best for compact data │
└─────────────────────────────────────────────────────────┘
Table Redefinition Error
┌─────────────────────────────────────────────────────────┐
│ INVALID TOML │
│ │
│ [fruit] │
│ apple = "red" │
│ │
│ [fruit] ◄── second definition │
│ orange = "orange" │
│ │
│ Parser error: table fruit defined more than once │
│ │
│ Fix: combine into one section │
│ [fruit] │
│ apple = "red" │
│ orange = "orange" │
└─────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| Key-value syntax | key = value |
| Bare key characters | ASCII letters, digits, _, - |
| Quoted key | Any string in "..." or '...' |
| Dotted key | Creates implicit tables |
| Standard table | [table] header, spans to next header |
| Nested table | [parent.child] header |
| Inline table | { key = value }, not extendable |
| Root table | No header; ends at first [table] |
| Redefinition | Invalid; each table defined once |
| Intermediate tables | Auto-created by dotted headers |
Key takeaways:
- A TOML document is a hash table with a root and nested tables. Key-value pairs fill the root until a header appears; the header switches context, and every following pair belongs to that table .
- Keys can be bare, quoted, or dotted. Bare keys have restricted characters; quoted keys accept any string; dotted keys create nested tables implicitly and cannot later be opened with a header .
- Tables cannot be redefined. A second
[fruit]header is an error, not a merge. The rule forces a single definition per table and makes the structure deterministic . - Inline tables are self-contained. They define all their keys at the braces and cannot be extended. Standard tables are for configuration sections that may grow .
- The root table is unnamed and closes at the first header. Entries after a header belong to that table, not to the root. This is why root-level configuration must appear before any
[section].
Remember: TOML’s syntax is a single assignment per line, but its structure is determined by headers and dotted keys. A key-value pair before any header goes into the root table. A [table] header starts a new context that lasts until the next header. A dotted key like a.b.c = 1 creates a and a.b without headers, and those implicit tables cannot be reopened. When a file fails to parse, check for a table defined twice, a dotted-key table opened with a header, or an inline table extended with a standard header. The syntax is simple; the composition rules are what require attention.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!