You have configuration as JSON: exported from a settings screen, generated by a script, or copied from documentation that only shows JSON examples. The tool you want to feed it to, however, expects TOML. Static site generators such as Hugo accept TOML config, Rust's Cargo requires it, Python packaging uses pyproject.toml, and many command-line tools read .toml files.
Converting in this direction raises questions that the reverse direction does not. TOML has no null. A TOML document cannot be an array. And TOML cares about the order in which values and tables are written. This guide explains each of these, shows what well-formed TOML output looks like, and covers the choices you make along the way.
How JSON structures become TOML
Take this JSON:
{
"title": "Blog",
"outputs": ["HTML", "RSS"],
"params": {
"author": "Jordan",
"social": { "github": "jkim" }
},
"menu": {
"main": [
{ "name": "Posts", "weight": 1 },
{ "name": "About", "weight": 2 }
]
}
}
A good converter produces:
title = "Blog"
outputs = ["HTML", "RSS"]
[params]
author = "Jordan"
[params.social]
github = "jkim"
[[menu.main]]
name = "Posts"
weight = 1
[[menu.main]]
name = "About"
weight = 2
The mapping rules are:
- Strings, numbers and booleans become
key = valuelines. - Arrays of simple values become inline arrays:
outputs = ["HTML", "RSS"]. - Nested objects become
[tables], with dotted headers for deeper levels such as[params.social]. - Arrays of objects become arrays of tables, one
[[menu.main]]block per object. - Arrays that mix objects with other values, or arrays of arrays, stay inline, with objects written as inline tables:
mixed = [1, { a = 1 }].
Why order matters in TOML
In JSON, the order of keys is irrelevant. In TOML, every key = value line belongs to the most recent table header above it. That means simple values for a table must be written before any sub-table headers. If a converter wrote [params.social] and then author = "Jordan", the author would end up inside social.
A correct serialiser therefore reorders output: plain values first, then sub-tables, then arrays of tables. Do not be surprised if your keys come out in a different order from the JSON; the structure is the same.
The null problem
JSON's null has no TOML equivalent. The specification deliberately leaves it out: a key is either present with a value or absent. You have two options:
- Leave the key out. Usually right for configuration, because most programs treat a missing key as "use the default", which is what
nulltypically meant. - Write an empty string (
key = ""). Useful when the consuming program requires the key to exist, but be careful: an empty string is a value, and some programs will try to use it.
Decide per case. For example, "analyticsId": null is best omitted; writing analyticsId = "" might make a site template output an empty tracking snippet.
Other things TOML cannot express
A root array. A TOML document is always a table. JSON like [{"name": "a"}, {"name": "b"}] must first be wrapped in an object, for example {"items": [...]}, which then becomes [[items]] blocks.
Huge nesting. Deeply nested JSON produces long dotted headers such as [a.b.c.d]. It is valid, but a sign the data might be better kept as JSON.
Dates as dates. JSON dates are strings, so they stay quoted strings in TOML. If the consuming program expects a real TOML date-time, edit those values to remove the quotes, for example published = 2024-06-01.
Keys with special characters. Keys that are not plain letters, digits, dashes and underscores must be quoted, as in "display name" = "Jordan" or "a.b" = 1 (the quotes stop the dot from creating a nested table). A proper converter does this for you.
Convert JSON to TOML with our tool
- Open the JSON to TOML Converter and paste your data into JSON input, use Open file, or click Load sample for a blog configuration example.
- Choose how to handle null values (TOML has no null): Leave the key out or Write as empty string "".
- Decide on Multi-line strings as """…""". With it on, strings containing line breaks are written as readable multi-line strings; with it off, they stay on one line with
\nescapes. - Review the TOML output and click Copy or Download to save a
.tomlfile.
If the JSON is invalid, you get the error with its line and column. If the top level is an array or a single value, the tool explains that a TOML document must be an object.
Checking the result
Always round-trip important config: paste the TOML output into the TOML to JSON Converter and compare the result with your original using JSON Compare. The only expected differences are removed nulls (or nulls turned into empty strings).
Then load the file in the actual program. A syntactically valid TOML file can still use keys the program does not recognise, and only the program, or its documentation, can tell you that.
Other ways to convert
Python. The standard library can read TOML (tomllib, Python 3.11+) but not write it. Writing needs a third-party package such as tomli-w or tomlkit; the latter can also preserve comments when editing an existing file.
Node.js. Several npm packages serialise objects to TOML. Check how each handles null and key ordering; behaviour varies, and some throw on null.
Command line. The Go-based yq tool can output TOML for many inputs, which is handy in scripts, though support for complex structures such as arrays of tables varies between versions, so check its documentation.
Write it by hand. For a config file with a dozen keys, hand-writing TOML is often quicker than converting, and lets you add comments that explain each setting.
Tips for readable TOML
- Add comments (
# ...) after converting. They are TOML's biggest advantage over JSON for config, and a converter cannot invent them. - Group related settings into tables rather than long dotted keys at the top level.
- Keep secrets out of the file. Use environment variables or a secrets manager and reference them from the config where the tool supports it.
- If the same data also needs to exist as YAML, JSON to YAML gives you that format from the same source.
Frequently asked questions
Does TOML support null?
No. TOML has no null value. Omit the key, or use a placeholder such as an empty string if the program requires the key to be present.
How do I represent a JSON array of objects in TOML?
As an array of tables: one [[name]] header per object, each followed by that object's key/value pairs.
Why did my keys change order after converting?
TOML requires simple values to appear before sub-tables within each table, so a correct converter writes them first. The data is identical; only the layout differs.
Can a TOML file start with an array?
No. The root of every TOML document is a table. Wrap a top-level JSON array in an object with a key, such as items.
Can I convert JSON with comments to TOML?
Standard JSON cannot contain comments, so remove them first. After converting, add comments to the TOML file, where they are allowed.