Compare TOML and JSON

TOML vs JSON: how the two formats differ in data types, comments, nesting and conversion, what is lost when you convert between them, and how to choose by situation.

TOML and JSON can both describe nested data as plain text, but they are built for different jobs. JSON is a compact interchange format that programs produce and consume. TOML is a configuration format meant to be written and edited by people. This guide covers what changes when you move data from one to the other, how to pick the right format, and what can go wrong when you copy, paste or convert. The behavior described comes from the published TOML and JSON specifications. The examples are illustrative and were not produced by any particular converter or tested in a specific tool.

What changes when data moves between TOML and JSON

Converting between TOML and JSON rewrites the syntax. Ideally the data stays the same. Keys, strings, numbers, booleans, arrays and nested structures exist in both formats, so most of a typical configuration survives a round trip. The formats do not have identical data models, though. Some values exist in only one of them, and some features are presentation-only and disappear when you convert.

The parts that normally carry over: key names, string values, booleans, arrays, and nested objects. A nested object in JSON corresponds to a table in TOML.

The parts that change or are lost:

  • Comments: TOML allows comments that start with #. JSON has no comment syntax, so comments are dropped when you convert TOML to JSON.
  • Dates and times: TOML has native offset date-times, local date-times, local dates and local times. JSON has no date type, so a converter usually writes these as strings, and the information that they were dates is lost.
  • Null: JSON has null. TOML has no null value, so a JSON-to-TOML conversion must either drop the key or substitute some other value, and either choice changes the data.
  • The top-level value: a TOML document is always a table of key/value pairs. JSON allows any value at the top level, including an array or a single string. A JSON document whose root is an array cannot be expressed in TOML unless it is wrapped under a key.
  • Special floats: TOML allows inf and nan. JSON has no way to represent them.
  • Number handling: TOML separates integers from floats and defines integers as 64-bit signed values. JSON has one number type and sets no precision limit, but parsers that store numbers as double-precision floats cannot represent every integer above 2^53 exactly. Large IDs can therefore change silently in some JSON tooling.
  • Layout and style: indentation, blank lines, choice of quote style, inline versus standard tables, and number formatting (TOML allows underscores and hex, octal or binary integers) are generally not preserved through conversion.

The same data in both formats

The following matched pair shows one small configuration written in TOML and the JSON a converter would typically produce from it. This output is illustrative, not the observed result of a specific tool.

# Service settings
title = "Inventory service"

[server]
host = "127.0.0.1"
port = 8080
started = 2024-05-01T09:30:00Z

[[users]]
name = "admin"
roles = ["read", "write"]

[[users]]
name = "viewer"
roles = ["read"]
{
  "title": "Inventory service",
  "server": {
    "host": "127.0.0.1",
    "port": 8080,
    "started": "2024-05-01T09:30:00Z"
  },
  "users": [
    { "name": "admin", "roles": ["read", "write"] },
    { "name": "viewer", "roles": ["read"] }
  ]
}

Comparing the two shows three things. The comment is gone. The value for started is now an ordinary string, so a program reading the JSON has no way to tell it is a timestamp unless the program already expects one. TOML's [[users]] array-of-tables syntax has become a JSON array of objects. The TOML version is easier to scan and annotate. The JSON version uses more punctuation but shows the nesting explicitly with braces and brackets.

Which format suits your situation

Criterion | TOML | JSON
Primary purpose | Human-edited configuration | Data interchange between programs
Comments | Yes, with # | No
Root value | Always a table | Any value, including arrays
Null | Not available | Available
Dates and times | Native types | Strings only, by convention
Integer vs float | Distinct types; integers are 64-bit signed | Single number type; precision depends on the parser
Duplicate keys | Invalid | The specification says names should be unique, but parser behavior with duplicates varies
Deep nesting | Becomes verbose, with long dotted keys or many table headers | Compact and explicit
Whitespace and line breaks | Key/value pairs end at a newline | Insignificant; the whole document can be minified to one line
Machine generation | Possible, but less common | Very common and widely supported

Choose TOML when people edit the file by hand, when comments explaining settings matter, when the data is fairly shallow (sections of key/value settings), or when you want dates kept as dates instead of strings.

Choose JSON when programs exchange the data, when the structure is deeply nested or irregular, when you need null or a top-level array, or when the data has to fit on a single line, such as in a log entry, an environment variable or an API payload.

Converting is reasonable when one side is a human-edited file and the other is a tool that only reads JSON. Treat the TOML file as the source you edit, and regenerate the JSON from it. Converting JSON back to TOML will not restore comments, and it requires a decision about any null values.

Whether the result stays readable and usable

Both formats are plain text, and both are expected to be encoded as UTF-8. TOML requires UTF-8. JSON exchanged between systems is expected to use it. Because they are plain text, they survive copying and pasting better than styled or decorated text does. A few common problems still break them.

  • Smart quotes: word processors, some chat apps and some note apps replace straight double quotes with curly ones. Neither format accepts curly quotes as string delimiters, so the pasted text will fail to parse.
  • Non-standard spaces: copying from web pages or rich-text documents can bring in non-breaking spaces. Both specifications define whitespace narrowly (spaces and tabs, plus line breaks in JSON), so these characters can cause parse errors that are hard to see.
  • Lost line breaks: TOML depends on newlines to separate key/value pairs, so pasting it into a single-line field breaks it. JSON can be collapsed onto one line without changing its meaning.
  • Trailing commas and comments in JSON: some editors and tools accept them, but standard JSON does not. A file that works in one tool can fail in a strict parser.
  • Number precision: a JSON value that parses without errors can still have changed if the receiving parser rounds large integers.

Before you rely on pasted or converted output, run it through a validator for the target format, or through the parser that will actually read it. Then confirm that the values you care about, especially timestamps, large numbers and any former null values, come out the way you expect. Validating only the syntax will not detect a timestamp that became a string or an integer that was rounded.

On accessibility: no assistive-technology testing was available for this guide, so there is no evidence here about how screen readers handle either format. JSON uses more punctuation and TOML uses more line-oriented sections. If people using screen readers will need to review the files, test with the readers and settings they use.

On sensitive data: configuration files often contain passwords, API keys, tokens and internal hostnames. A web-based formatter or converter may send whatever you paste to a remote server, and you usually cannot confirm from the page itself whether processing happens in your browser. For any file containing secrets, use a local tool or a library in your own environment, or remove the secrets before pasting.

Gaps in this comparison

This guide describes the formats as their specifications define them. It does not report tests of specific converters, editors or validators, which can differ in how they handle dates, key order, large numbers and non-standard extensions. Check the documentation of the tool you plan to use, or run a sample file through it, before relying on it for production data.