TOML basics: keys, strings, numbers and booleans
TOML is designed for configuration files that people read and edit by hand. It does not stack braces like JSON, and it does not use indentation for structure like YAML. The basic unit is one line of key = value, and a table opens with square brackets. This article covers the rules for writing a single value correctly.
Three ways to write a key
- Bare key: only
A-Z,a-z,0-9,_and-.server,max_upload_mbandx-frame-optionsare all bare keys. - Basic string key: wrapped in double quotes, as in
"key with spaces" = 1. Escapes work. - Literal string key: wrapped in single quotes, as in
'c:\path' = 1. Backslashes stay as they are.
A dot inside a key creates a nested table. server.port = 8080 is the same as putting port under [server], with one catch: a table created by a dotted key cannot later be reopened with [server]. The tables article covers that rule in detail.
server.port = 8080 server.host = "127.0.0.1" # the [server] table is created implicitly
Four kinds of string
| Form | Syntax | Escapes | Used for |
|---|---|---|---|
| Basic | "text" | yes (\n, \t, \u00E9) | most short values |
| Multi-line basic | """text""" | yes, plus a trailing \ to join lines | descriptions, JSON fragments |
| Literal | 'text' | none | Windows paths, regular expressions |
| Multi-line literal | '''text''' | none | shell scripts, escape-heavy text |
A multi-line basic string drops the single newline immediately after the opening quotes, so the two values below are identical. A backslash at the end of a line removes that newline and the leading whitespace of the next line, which is how long sentences are split for readability.
a = """ first second""" b = "first\nsecond"
Literal strings have no escapes. 'C:\new' keeps the backslash and the n. In a basic string, by contrast, an escape such as \q that the specification does not define is an error.
Numbers: integers and floats
Integers can be decimal, hexadecimal (0x), octal (0o) or binary (0b). Underscores may separate digits for readability. The value must fit the signed 64-bit range; anything larger is an error.
42,+17,-0and1_000_000are integers.0xDEAD_BEEF,0o755and0b1010_0110are integers too, and cannot take a sign.01and1_are errors: a leading zero and a trailing underscore are not allowed.
A float must have a decimal point or an exponent. 1.0, -0.0, 3.14, 5e+22 and 1e-7 are all floats. inf, +inf, -inf and nan are valid values as well.
ratio = 0.375 limit = inf missing = nan count = 1_024 # integer approx = 1_024.0 # float
1. and .5 are errors, not floats: there must be digits on both sides of the point, and a leading zero is not allowed. A version written as a number, such as 0.1.0, is read as 0.1 and then fails on the rest. Write versions as strings.
Booleans and null
There are exactly two booleans, true and false. True or TRUE is read as a key or rejected. TOML has no null. To express an absent value, omit the key or use the empty string "".
Comments and line breaks
A comment runs from # to the end of the line. A # inside a string is not a comment. Whitespace around keys, values and comments is free, but a statement must finish on one line. Long values may only wrap inside arrays and inline tables.
The mistakes beginners make most
- Joining key and value with
:. TOML only uses=. - Leaving a string unclosed, so the next line is swallowed and the reported position looks odd.
- Writing the same key twice in a table. The later value does not win; it is an error.
- Writing a version without quotes:
version = 1.2.3is an error.
This tool reports every one of those with a line and column, names the key involved, and moves the caret to that line when you select the diagnostic, so the position never has to be found by hand.
Questions
How do I put a space in a key?
Quote it: <code>"my key" = 1</code> is valid TOML. Where possible, prefer a name that works as a bare key so other tools find it easily.
Can TOML express null?
No. Omit the key when a value is absent. When converting from JSON, this tool skips a null key and reports a warning.
Are underscores allowed in numbers?
Only between digits. <code>1_000</code> is fine; <code>1__000</code>, <code>_1000</code> and <code>1000_</code> are errors.
Paste your TOML to see immediately which type each value is read as.
Open the toolThese 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.