opyeb tomllab

tomllab · Structure

TOML tables, dotted keys and arrays of tables

Table headers, dotted keys, inline tables, [[array]], redefinition rules

Almost every TOML value lives inside a table. The whole document is one top-level table, and named tables nest beneath it. There are four ways to build structure, and they follow different rules.

1. The bracketed table

[server] switches the current table to server for the lines below it. Dots open nested tables in one header: [server.tls] is the tls table inside server.

[server] host = "127.0.0.1" [server.tls] enabled = true

Parent tables do not have to be declared first. [a.b.c] alone implicitly creates a and a.b. Declaring [a] afterwards is allowed, but declaring [a.b] again is an error.

2. Dotted keys

a.b.c = 1 builds tables inside the key. It produces the same structure without brackets, which suits short settings. A table created this way counts as explicitly defined, so it cannot be reopened later with [a.b].

[fruit] apple.color = "red" apple.taste.sweet = true # [fruit.apple] <- error # [fruit.apple.taste] <- error [fruit.apple.texture] # a new sub-table is fine smooth = true

3. Inline tables

{ key = value } builds a table on one line. It exists only as a value and cannot gain keys afterwards. An inline table may not contain a newline and may not end with a comma; keep it for short, closed structures.

point = { x = 1, y = 2 } # point.z = 3 <- error: an inline table cannot be extended

4. Arrays of tables

[[route]] stacks tables of the same name into an array. Each declaration appends a new element, and the lines below fill that element. It suits lists whose entries share a set of keys: servers, routes, database bindings.

[[route]] path = "/" method = "GET" [[route]] path = "/health" method = "GET"

A sub-table inside an array of tables is written [route.cache] and refers to the last element. When the next [[route]] begins, sub-tables start applying to the new element.

Redefinition rules at a glance

WriteLater, same nameResult
[a][a]error (defined twice)
[a.b][a]allowed (the parent was implicit)
a.b = 1[a]error (the dotted key defined it)
a = {x = 1}[a] or a.y = 2error (inline tables are closed)
[[a]][[a]]allowed (appends an element)
[[a]][a]error (array cannot become a table)

These rules come from the TOML specification, and this tool was verified to make the same decisions as Python's tomllib. Each error message names the key or table involved and gives its line and column.

Choosing a structure

Questions

Can I write [a] after [a.b]?

Yes. The parent table is created implicitly and may be declared explicitly once. Writing [a] and then [a] again is the error.

Can I add a key to an inline table later?

No. An inline table closes when its value is created. If it may grow, declare it as a bracketed table.

Are [[a]] and a = [] the same?

No. [[a]] is an array of tables that can keep gaining keys underneath. a = [] is an empty array value and cannot become an array of tables.

Load the nested sample and see how the type table separates tables from arrays of tables.

Open the tool

These guides follow the TOML v1.0.0 specification (2021-01-11), RFC 3339 (dates and times), the YAML 1.2 core schema and JSON (RFC 8259). Every example is synthetic.