Rules for converting JSON to TOML
TOML can express less than JSON. It has no null and its top level must be a table. Knowing just those two rules covers most conversions.
The one thing TOML lacks: null
TOML has no null type. A key whose value is null is dropped from the TOML output and the tool reports how many were dropped.
An empty string is not null. "" is a valid TOML value and stays as it is.
| JSON | TOML | Note |
|---|---|---|
| "a": null | key omitted | cannot be expressed |
| "a": "" | a = "" | kept |
| "a": [] | a = [] | kept |
| ["x", 1] | not convertible | top level is not a table |
Nested objects become tables
A JSON object becomes a TOML table header and an array of objects becomes an array of tables ([[ ]]). Scalars are collected before the first table header.
Keys containing spaces or dots are quoted so the original name survives.
{"server":{"host":"0.0.0.0","port":8080},
"tags":["a","b"],
"route":[{"path":"/x","weight":1},{"path":"/y","weight":2}]}
=> tags = ["a", "b"]
[server]
host = "0.0.0.0"
port = 8080
[[route]]
path = "/x"
weight = 1
[[route]]
path = "/y"
weight = 2
Keep numbers and strings apart
In JSON, "300" and 300 are different values, and so they are in TOML. Check that no quotes disappeared in the conversion; environment variable values are almost always strings.
Dates have no JSON type and stay strings. If a TOML reader should see a real date, edit the output to an unquoted YYYY-MM-DD value.
Questions
Can a JSON document with a top-level array become TOML?
A TOML document's top level must be a table. Move the array under a key, for example {"items": [...]}, and it converts.
Is turning null into an empty string acceptable?
It changes the meaning: null is 'no value', an empty string is 'an empty value'. If the consumer distinguishes them, omitting the key is safer.
Sources
Every example here can be pasted into the converter as it is.
Open the converter