TOML to YAML

Parse and convert TOML to YAML.

loading TOML to YAML
About this tool

When do you convert TOML to YAML?

TOML and YAML often meet at the border between two toolchains. A Rust or Python project keeps its settings in Cargo.toml or pyproject.toml; the CI pipeline, the Helm chart or the deployment tool next to it wants YAML. A static site starts in Hugo's TOML and moves to a generator that reads YAML front matter. A team standardises all its configuration on one format. In each case somebody has to rewrite the file, and a faithful rewrite means keeping the structure while changing the syntax.

The conversion is mostly a change of notation. A [server.tls] header becomes two levels of indentation, a [[routes]] block becomes a list of mappings, and key = "value" becomes key: value. What deserves attention are the values that mean different things in each format: TOML dates are real dates and YAML timestamps are written differently, and unquoted words such as NO or on can be read as booleans by older YAML parsers.

This converter runs in your browser and uploads nothing. It reads TOML 1.0, writes YAML 1.2, expands inline tables and dotted keys, and keeps integers beyond 2^53 exact. Comments are not part of parsed data, so they are not carried over.

How to convert TOML to YAML

  1. Paste the TOML in the input box. The page opens with a small sample of a project configuration, and the direction, TOML to YAML, is fixed at the top.
  2. The YAML appears in the output box as you type: tables become nested mappings and arrays of tables become lists with - markers.
  3. Set the Indent to 2 or 4 spaces. A tab is offered but YAML does not allow tabs, so that choice is written as 2 spaces. There is no minify option for YAML.
  4. Tick Sort keys to order every mapping alphabetically, or leave it off to keep the TOML order.
  5. Press Copy or Download to save converted.yaml.
  6. If the TOML is invalid, the red box shows the line and column and the reason, with the line of your input underneath. Fix it and the output returns.

TOML to YAML examples

A Cargo.toml turned into YAML

Input:

[package]
name = "demo"
version = "0.3.1"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = "1"

[[bin]]
name = "cli"
path = "src/main.rs"

[[bin]]
name = "server"
path = "src/server.rs"

Output:

package:
  name: demo
  version: 0.3.1
  edition: "2021"
dependencies:
  serde:
    version: "1.0"
    features:
      - derive
  tokio: "1"
bin:
  - name: cli
    path: src/main.rs
  - name: server
    path: src/server.rs

Strings that look like numbers ("2021", "1.0", "1") keep their quotes, so YAML still reads them as text. 0.3.1 is not a number, so it needs none.

Nested tables and tables in arrays

[server], [server.tls] and two [[server.routes]] blocks become:

server:
  host: localhost
  tls:
    enabled: true
    cert: a.pem
  routes:
    - path: /
    - path: /api

Dates, times and number forms

day = 2024-05-01, odt = 1979-05-27T07:32:00Z and under = 1_000_000 give day: 2024-05-01, odt: 1979-05-27T07:32:00.000Z and under: 1000000. The dates are written without quotes, so a YAML reader that knows timestamps will see dates. hex = 0xFF becomes 255, and a float such as f = 3.0 is written as 3.

Words that old YAML parsers misread

answer = "no" and country = "NO" come out as answer: no and country: NO, unquoted, because YAML 1.2 reads them as text. A YAML 1.1 parser such as PyYAML turns both into false. If the target reads YAML 1.1, add the quotes by hand.

Multi-line strings

A TOML """ string with two lines becomes a YAML literal block: text: |- followed by the two lines indented.

Common errors and how to fix them

  • Invalid TOML document: trying to redefine an already defined table or value (line 3, column 2): the same [server] header or key appears twice. Merge the two blocks into one.
  • Invalid TOML document: invalid value (line 1, column 8): a string missing its quotes, as in name = demo. Write name = "demo".
  • Invalid TOML document: illegal character in key (line 1, column 5): the line uses a colon, name: demo, the YAML way. TOML wants name = "demo".
  • Invalid TOML document: expected comma or end of structure: an array that is not closed, or a missing comma between its items.
  • The output is not what the target expects: the conversion is valid, but the tool reading the YAML may want another shape, such as a list where TOML had a table. Compare with the tool's documentation, since the two formats leave the layout of the data to each application.

Frequently asked questions

How do I convert TOML to YAML online?

Paste the TOML in the input box, read the YAML and press Copy or Download. It happens in your browser, with no account. For the reverse, use YAML to TOML.

Are comments kept when converting?

No. Comments are not data, so the parser throws them away, and neither JSON nor YAML output carries them. Add them to the YAML again by hand where they matter.

Does TOML to YAML change my numbers?

Integers stay integers and large ones keep every digit. Floats are rewritten in their shortest form (3.0 becomes 3), and hexadecimal, octal and binary integers appear in decimal. If a float must keep its .0, edit it in the result.

Can I convert through JSON?

You can, and the data is identical. TOML to JSON shows the structure with its braces, which is useful to debug; this page goes straight to YAML. To tidy the result afterwards, use the YAML formatter.

Which is better for configuration, TOML or YAML?

TOML is stricter and has fewer surprises: no indentation rules, explicit types. YAML is more compact for deep data and is what most infrastructure tools expect. Pick the one the consuming tool reads, then convert once and keep that file as the source.

How to convert TOML to YAML in code

JavaScript

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

const data = parse('[server]\nhost = "localhost"\n[[server.routes]]\npath = "/"\n');
console.log(stringify(data));

Python

tomllib (Python 3.11 and later) reads TOML; PyYAML writes the YAML.

import tomllib
import yaml

with open("config.toml", "rb") as f:
    data = tomllib.load(f)
print(yaml.safe_dump(data, sort_keys=False, allow_unicode=True))

Bash

yq -p toml -o yaml '.' config.toml     # mikefarah/yq v4
Report a problem