JSON, YAML, TOML and XML: which format when
All four store the same kind of thing, nested keys and values, but each was designed for a different reader. JSON is for programs talking to programs, YAML and TOML are for people editing configuration, and XML is for documents and the systems that grew up around them. This guide covers what each is good at, the traps that catch people, and what happens to your data when you convert between them.
By Radhika Singh · Last updated
The short answer
Pick by who reads and writes the file most often. If it is a program, use JSON. If it is a person editing settings, use YAML when the settings nest deeply and TOML when they are mostly flat. Use XML when a standard or an existing system already requires it.
| Format | Best for | Comments | Types | You see it in |
|---|---|---|---|---|
| JSON | APIs, data exchange, browser storage | No | String, number, true/false, null, array, object | REST APIs, package.json, web app state |
| YAML | Configuration people edit, with deep nesting | Yes (#) | JSON's types, guessed from the text unless quoted | Kubernetes, GitHub Actions, Docker Compose |
| TOML | Configuration that is mostly flat | Yes (#) | Explicit, including dates and times | Cargo.toml, pyproject.toml |
| XML | Documents and long-lived integrations | Yes (<!-- -->) | Text, unless a schema says otherwise | SOAP, RSS and sitemaps, Office files, SVG |
JSON: the default for data between programs
JSON (RFC 8259) has six kinds of value and very few rules, which is why every language reads it. The rules it does have are strict: keys must be in double quotes, trailing commas are errors, and there are no comments. A config file that needs explanations is the clearest sign JSON is the wrong choice for it.
One trap matters in practice: JavaScript reads every JSON number as a 64-bit float, so integers above 9,007,199,254,740,991 (2^53 − 1) silently lose precision. IDs from databases and payment systems are often sent as strings for exactly this reason. Keep them as strings when you convert.
For logs and large exports, JSON Lines puts one complete object on each line so a file can be streamed and appended; the JSON Lines converter turns an array into lines and back. To ship JSON smaller, minify it; to put JSON inside a string, such as a config value or a test fixture, escape it; to pull values out of a large response, try a JSONPath expression in the JSONPath tester or a jq filter in the jq playground.
YAML: configuration written by people
YAML replaces JSON's brackets with indentation and adds comments, multi-line strings and anchors (define a block once, reuse it with an alias). YAML 1.2 was designed so that valid JSON is also valid YAML, which is why converting JSON to YAML is safe while the reverse can surprise you.
The surprises come from implicit typing: YAML decides what a value is from how it looks. Under YAML 1.1 rules, which many parsers still follow, yes, no, on and off become booleans, so a list of country codes turns NO (Norway) into false. A version written 3.10 becomes the number 3.1. Quote anything that must stay text. Indentation must use spaces; a tab in the indentation is an error.
Before committing a CI or Kubernetes file, run it through a YAML validator. To see exactly what a parser will read, convert it with YAML to JSON: the JSON shows every guessed type explicitly. JSON to YAML goes the other way when a tool wants YAML and your data is JSON.
TOML: flat configuration with explicit types
TOML (version 1.0.0, January 2021) looks like an INI file with real types: key = value lines, [table] headers for sections, and native dates and times, so 2026-11-29 is a date and not a string to parse later. Nothing is guessed: strings are always quoted, so the Norway problem cannot happen.
It is the standard for Rust packages (Cargo.toml) and Python projects (pyproject.toml). Its weakness is deep nesting: three or four levels means long dotted headers such as [server.http.tls], and lists of tables use the [[double bracket]] syntax that newcomers often misread. To check how a TOML file is structured, convert it with TOML to JSON.
XML: documents, schemas and long-lived systems
XML is markup first: elements can contain text, other elements and attributes, which suits documents (SVG images, Office files, which are zipped XML, RSS feeds and sitemaps) and the enterprise integrations built on SOAP and XML Schema. Namespaces and schemas make it verbose, but they also make it possible to validate a message against a contract.
Converting XML to JSON has two decisions with no single right answer. Attributes and child elements both become keys, so a converter marks attributes with a prefix. And an element that appears once looks like a single object, while the same element repeated becomes an array, so code that reads the JSON must handle both. The XML to JSON converter lets you set both, attributes as @name keys and repeated elements always as arrays, so the shape is predictable before you write code against it; JSON to XML builds XML from JSON when a system expects it.
If you parse XML in your own code, turn off DTD and external entity processing. Entity expansion is the basis of the XXE and billion laughs attacks; this site's converter removes the DOCTYPE before parsing for that reason.
Spreadsheets and TypeScript
Two conversions come up constantly at the edges of these formats. People who live in spreadsheets need JSON as rows and columns: JSON to Excel flattens an array of objects into a sheet, and Excel to JSON reads a sheet back as objects, including Excel's serial dates. Developers who receive JSON need types: JSON to TypeScript writes interfaces from a sample. Types inferred from one sample are only as complete as that sample, so feed it a response that includes optional fields.
A checklist for choosing
When the choice is still open:
- Programs on both ends, no human editing: JSON.
- People edit it and it needs comments: YAML or TOML, never JSON.
- Deep nesting (Kubernetes manifests, CI pipelines): YAML. Mostly flat settings: TOML.
- A standard, a partner or an existing system requires it: XML, and validate against its schema.
- Values that must stay exact (IDs, versions, ZIP codes): store them as quoted strings in every format.
Whatever you pick, convert once at the boundary and keep one format inside your own code.