TOML vs JSON: Config Format Differences and Examples
Last updated:
TOML is the right format for human-written config files; JSON is the right format for machine-to-machine data interchange — the 2 formats solve different problems and choosing the wrong one adds friction. TOML supports comments, 4 native datetime types, and trailing commas in arrays; JSON supports none of those. JSON has 1 hard conversion blocker when moving to TOML: null has no TOML equivalent and must be removed or replaced before conversion. This guide covers 5 key topics: syntax side-by-side, the full type mapping table, comment handling, the null problem, and when to pick each format for package metadata, CI config, REST APIs, and browser storage.
Same config in TOML and JSON
name = "jsonic"
debug = false
[server]
host = "localhost"
port = 3000{
"name": "jsonic",
"debug": false,
"server": {
"host": "localhost",
"port": 3000
}
}Quick comparison
| Feature | TOML | JSON |
|---|---|---|
| Best for | Human-edited config | APIs and machine interchange |
| Comments | Supported | Not allowed |
| Trailing commas | Allowed in arrays | Not allowed |
| Nesting | Tables and dotted keys | Objects and arrays |
| Browser support | Parser library needed | Built in |
Choose TOML when humans edit the file
TOML is a strong fit for package metadata, build settings, service config, and CLI tools. Comments let teams explain values inline, and tables avoid deeply nested braces.
# Production server
[server]
host = "0.0.0.0"
port = 8080
workers = 4Choose JSON when data crosses system boundaries
JSON is the safer default for web APIs, browser storage, fixtures, and integration tests. It is strict, widely supported, and maps directly to JavaScript objects.
If strict syntax is causing errors, the fix invalid JSON guide covers common issues like trailing commas, comments, and single quotes.
Migration tips
- Convert comments into documentation because JSON cannot preserve them.
- Turn TOML tables into nested JSON objects.
- Check dates explicitly because TOML has date/time types and JSON does not.
- Validate the result before using it in a build or deployment pipeline.
Convert JSON to TOML
Use Jsonic's JSON to TOML converter to turn JSON objects into TOML tables and compare the output with your existing config style.
Open JSON to TOMLFrequently asked questions
Can TOML represent the same data as JSON?
Mostly yes. Both support strings, numbers, booleans, arrays, and nested objects. TOML adds native datetime types. The key difference: TOML cannot represent null, and the root value must be a table (object), not an array.
Does TOML have a formal specification?
Yes. TOML v1.0.0 is the stable spec at toml.io, finalized in 2021. Unlike YAML, which has incompatible 1.1 and 1.2 versions, TOML has a single stable specification that well-implemented parsers follow consistently.
What TOML types does JSON not support natively?
TOML has four native datetime types (offset datetime, local datetime, local date, local time) and distinguishes integers from floats explicitly. JSON represents dates as strings and has no standard datetime type. TOML also supports inf and nan float values; JSON does not.
When would I use TOML over JSON?
Use TOML for human-written config files: it supports comments, avoids trailing comma errors, and uses readable section headers instead of nested braces. Use JSON when config is generated programmatically, consumed by a browser, or shared across systems that expect JSON.
How do I convert TOML to JSON programmatically?
Python 3.11+: use tomllib to parse and json.dumps to serialize. For older Python, use the tomli package. In Node.js, use smol-toml or @iarna/toml. Note that TOML datetimes become strings in JSON and TOML null does not exist, so remove null values before converting.
Does TOML allow comments?
Yes. TOML uses # for single-line comments, same as YAML. Comments can appear on their own line or after a value. There is no multi-line comment syntax. JSON has no comment support at all, which is one reason TOML is preferred for hand-edited config files like Cargo.toml and pyproject.toml.
Recommended reading
- Designing Data-Intensive Applications (2nd Edition) — Martin Kleppmann & Chris RiccominiThe modern classic on data systems — encoding formats, schemas, replication, and stream processing.
- API Design Patterns — JJ GeewaxBattle-tested patterns for designing consistent, scalable JSON APIs — from a Google API architect.
As an Amazon Associate, Jsonic earns from qualifying purchases.