YAML to TOML

Parse and convert YAML to TOML.

loading YAML to TOML
About this tool

Why convert YAML to TOML?

YAML and TOML are both friendly configuration formats, but they grew up in different places. YAML is the language of Kubernetes, CI pipelines and Ansible. TOML (version 1.0.0) is what Rust's Cargo.toml, Python's pyproject.toml, Hugo, Netlify and many newer tools use. When a project moves from one world to the other, or when a tool you adopt only reads TOML, you have to rewrite the file, and doing it by hand is where indentation slips and misplaced brackets creep in.

The two formats are not mirror images. YAML nests with indentation; TOML nests with [table] headers, and a list of objects becomes a repeated [[array.of.tables]] header. TOML is stricter: it has no null, the top level must be a set of key = value pairs, and strings are always quoted. This converter handles the rewrite and tells you plainly when something in your YAML has no TOML equivalent, instead of producing a file the target tool would reject.

It runs in your browser, so the file you paste is not uploaded. It reads YAML 1.2, writes TOML, and keeps integers larger than 2^53 exact.

How to convert YAML to TOML

  1. Paste the YAML in the input box. The page opens with a small sample project config; the direction, YAML to TOML, is fixed at the top.
  2. The TOML appears in the output box as you type. Scalar values come first, then each nested mapping as a [table], then lists of mappings as [[tables]].
  3. Tick Sort keys if you want every table's keys in alphabetical order. TOML output has no indent or minify options, because TOML does not depend on indentation.
  4. Read the result once before using it. Strings are always written in double quotes, including dates and version numbers.
  5. Press Copy, or Download to get converted.toml.
  6. If a red box appears, it names the problem: a YAML syntax error with its line and column, or a value that TOML cannot hold, such as a null.

YAML to TOML examples

A config with a table and a list of tables

Input:

title: Example
owner:
  name: Maria Silva
  active: true
servers:
  - name: alpha
    ip: 10.0.0.1
  - name: beta
    ip: 10.0.0.2
ports: [80, 443]

Output:

title = "Example"
ports = [ 80, 443 ]

[owner]
name = "Maria Silva"
active = true

[[servers]]
name = "alpha"
ip = "10.0.0.1"

[[servers]]
name = "beta"
ip = "10.0.0.2"

Notice that ports moved above [owner]: in TOML every plain key must come before the first table header, so the converter writes scalars and lists of values first.

Deeply nested mappings

a: containing b: containing c: 1, with a sibling d: 2 and a top-level z: 3, becomes:

z = 3

[a]
d = 2

[a.b]
c = 1

Dotted headers such as [a.b] replace the indentation.

Dates arrive as strings

YAML released: 2024-05-01 is written as released = "2024-05-01", quoted. In TOML an unquoted 2024-05-01 is a real date value. If your target tool needs a date type, delete the quotes in the result.

A list of mixed values

a: [1, two, 3.5] becomes a = [ 1, "two", 3.5 ]. TOML 1.0 allows mixed types in an array, but some older parsers do not, so keep lists uniform when you can.

Common errors and how to fix them

  • TOML has no null value (at "proxy"): the YAML has proxy: ~ or proxy: null. TOML cannot express "nothing". Remove the key (a missing key is the TOML way of saying null) or give it a value such as an empty string. The message points to the exact path, for example items[0].b inside a list.
  • TOML needs an object at the top level: the YAML starts with a list (- a, - b) or is a single value. Wrap it under a key first, for example items: followed by the list.
  • Tabs are not allowed as indentation, All mapping items must start at the same column and Map keys must be unique are YAML mistakes in your input, reported with line and column. Fix the line shown in the red box.
  • Source contains multiple documents: a --- separator splits the file. TOML has one document per file, so convert each part separately and keep one file per document.
  • A number with too many digits: a decimal longer than a double can hold is refused rather than rounded. Quote it in the YAML.

Frequently asked questions

How do I convert YAML to TOML online?

Paste the YAML in the input box and copy the TOML from the output box, or download it as converted.toml. Nothing is uploaded. For the opposite direction, see TOML to YAML.

What cannot be converted from YAML to TOML?

Nulls, a list at the top level and numbers with more digits than a double. Comments, anchors and aliases also disappear: aliases are expanded into copies, and comments are not part of the parsed data, so write them again in the TOML.

Can I go through JSON instead?

Yes, and it is a good way to inspect the data in the middle. YAML to JSON shows what the parser understood, and JSON to TOML finishes the job. The result is the same as converting directly.

Does the order of keys change?

Keys stay in the order of the YAML, except that TOML needs plain values before tables and lists of values before sub-tables, so those move up. Turn on Sort keys for a fully alphabetical file.

Is TOML better than YAML for configuration?

It depends on the file. TOML is harder to get wrong because it has no significant indentation and no surprising types like no becoming false, but it gets awkward with deep nesting. YAML is more compact for deep structures and lists of objects.

How to convert YAML to TOML in code

JavaScript

import { parse } from 'yaml';
import { stringify } from 'smol-toml';

const data = parse('title: App\nowner:\n  name: Ana\nports: [80, 443]\n');
console.log(stringify(data));

stringify throws if the data has a null, so remove those keys first.

Python

tomllib in the standard library only reads TOML, so writing needs a package such as tomli-w.

import yaml
import tomli_w

with open("config.yml") as f:
    data = yaml.safe_load(f)
with open("config.toml", "wb") as out:
    tomli_w.dump(data, out)

Bash

yq -o toml '.' config.yml > config.toml     # mikefarah/yq v4
Report a problem