JSON versus TOML

How JSON and TOML differ in syntax, data types and readability, what changes when you convert between them, and how to choose the right format for configuration or data exchange.

JSON and TOML both store structured data as plain text, but they were designed for different jobs. JSON is a compact interchange format that machines produce and consume constantly. TOML is a configuration format meant to be written and edited by people. This guide explains what changes when data moves between them, how they compare, and how to check whether the converted text will work where you need it.

What converting between JSON and TOML actually changes

Converting a document from JSON to TOML, or back, rewrites the syntax completely. Every brace, bracket, quote and separator may change, and so may the order and grouping of lines. The goal is to keep the underlying data the same: the keys, values, nesting and types. This is different from cosmetic text effects such as reversed character order, mirrored glyphs or Unicode lookalike styling. Those change how text looks to a person. A format conversion changes the stored characters so that a different parser can read the same information.

The two data models overlap but are not identical, so a conversion is not always lossless. The items below usually carry across cleanly, and the ones after them need attention.

  • Usually preserved: string values, booleans, integers within range, floating-point numbers, arrays, and nested objects or tables.
  • Lost when going from TOML to JSON: comments, because JSON has no comment syntax.
  • Changed when going from TOML to JSON: dates and times, because JSON has no date type. They typically become strings, and a converter may or may not mark them as dates.
  • Blocked when going from JSON to TOML: null values, because TOML has no null. A converter must drop the key, substitute a value or raise an error.
  • Blocked when going from JSON to TOML: a top-level array or bare value, because a TOML document must be a table of key/value pairs at its root.
  • At risk in either direction: special float values such as infinity and NaN (allowed in TOML, not in JSON) and very large integers (TOML expects 64-bit signed integers, while many JSON parsers handle numbers as double-precision floats).

The same data in both formats

The two snippets below express the same configuration. They are written by hand to illustrate the syntax and are not the output of any specific converter; real tools may choose different layouts, quoting or ordering.

{
  "title": "Example service",
  "debug": false,
  "server": {
    "host": "127.0.0.1",
    "port": 8080
  },
  "admins": ["alice", "bob"],
  "backups": [
    { "path": "/var/backup/a", "keep_days": 7 },
    { "path": "/var/backup/b", "keep_days": 30 }
  ]
}
# Main settings
title = "Example service"
debug = false
admins = ["alice", "bob"]

[server]
host = "127.0.0.1"
port = 8080

[[backups]]
path = "/var/backup/a"
keep_days = 7

[[backups]]
path = "/var/backup/b"
keep_days = 30

In the TOML version, the nested server object becomes a [server] table header, and the array of backup objects becomes repeated [[backups]] headers. Plain keys such as admins sit above the first table header, because any key written after a header belongs to that table. The comment line has no equivalent in the JSON version and would disappear if the file were converted back.

How JSON and TOML compare

Criterion | JSON | TOML
Primary purpose | Data interchange between programs | Human-edited configuration files
Comments | Not allowed | Allowed, starting with #
Null value | Supported | Not supported
Dates and times | No native type; stored as strings | Native offset date-time, local date-time, local date and local time
Root of the document | Any value, including an array | Must be a table of key/value pairs
Nested structures | Braces and brackets at any depth | Table headers, dotted keys and inline tables; deep nesting becomes verbose
Trailing commas | Not allowed | Not allowed in inline tables; allowed in arrays
Duplicate keys | Spec says names should be unique; parser behavior varies | Invalid; a conforming parser must reject them
Infinity and NaN | Not allowed | Allowed
Integers | Range not fixed by the spec; precision depends on the parser | 64-bit signed; out-of-range values must be rejected
Whitespace and indentation | Not significant | Not significant
Encoding | UTF-8 for interchange | Must be valid UTF-8

Which format suits your intended result

Choose JSON when the data travels between programs: API requests and responses, logs, data exports, or anything a browser or service generates automatically. It handles null values, top-level arrays and deep nesting directly, and support for it is nearly universal across languages and tools. Its main drawbacks for hand-editing are the lack of comments and strict punctuation rules, where a single missing quote or extra comma makes the whole document invalid.

Choose TOML when people will read and edit the file by hand, especially for application or project settings. Comments let you document why a setting exists, table headers group related keys visibly, and native date types avoid ambiguity. TOML is used for project configuration in several established ecosystems, such as Rust's Cargo.toml and Python's pyproject.toml. It becomes awkward for deeply nested or highly irregular data, and it cannot represent null or a root-level list.

If a tool or platform you depend on expects one specific format, that requirement outweighs any general preference. When you control the choice, a useful test is to ask who writes the file most often. If software usually writes it, choose JSON. If people usually edit it, choose TOML.

Will the converted text stay valid and usable where you need it

Valid syntax is the first requirement, because a parser rejects the whole file over one error. After any conversion, run the result through a parser for the target format, ideally the same library and version your application uses. Implementations differ on edge cases such as duplicate keys, large numbers and newer syntax features. Check which TOML specification version your parser supports, because features have changed between versions; for example, TOML 1.0 allows arrays of mixed types, while earlier versions did not.

Round trips deserve a separate check. Converting TOML to JSON and back will drop comments, may turn dates into plain strings, and may reorder or regroup keys. Neither specification treats key order as meaningful, but people reading a configuration file often do. If comments or layout matter, keep the TOML file as the source of truth and generate JSON from it, rather than editing the JSON and converting back.

Copy and paste can quietly break both formats. Word processors, chat apps and some web pages may replace straight quotes with curly typographic quotes, or insert non-breaking spaces and other invisible characters. Neither parser accepts curly quotes as string delimiters. Paste into a plain-text or code editor, and if a file that looks correct fails to parse, check for these characters. A byte order mark at the start of a file can also cause problems with some parsers.

Both formats are ordinary plain text, so they avoid the accessibility and search problems of decorative Unicode lookalike characters. Readability still depends on formatting. Minified JSON on a single line is hard for anyone to follow, including people using screen readers or magnifiers, so pretty-print it before review. In TOML, clear table headers and short comments help most.

Configuration files often contain passwords, API keys, tokens or internal hostnames. Pasting them into an online converter or validator sends that content to a third party, where it may be logged or retained. Prefer a converter that runs locally, such as a command-line tool, an editor extension or a standard library in your programming language. Otherwise, replace secrets with placeholders before using an external service.

Limits of this comparison

This guide is based on the general rules of the two formats as their specifications define them. It does not include benchmark data, parser performance measurements or tests of particular converter tools, and the example snippets are illustrative rather than observed output. Before relying on a conversion in production, test it with your actual data, your target parser and the specification version that parser implements.