# YAML Formatter

Reindent and normalize messy YAML with consistent spacing. Pick indent width, sort keys, or switch between expanded block and compact flow style.

## Run it

- **CLI:** `gizza tool yaml-formatter "name:   gizza
tags: [a, b]
nested: {x: 1}"`
- **Web:** https://gizza.ai/tools/yaml-formatter/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/yaml-formatter/tool.json

## Inputs

- `yaml` — YAML to format _(field)_
- `indent` — Spaces per level (1–8) _(field)_
- `style` — Output style _(field)_
- `sort_keys` — Key order _(field)_

## Output

- Formatted YAML (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `yaml` — YAML to format
- `indent` — Spaces per level (1–8)
- `style` — Output style
- `sort_keys` — Key order

Example: `https://gizza.ai/tools/yaml-formatter/?yaml=name%3A%20%20%20gizza%0Atags%3A%20%5Ba%2C%20b%5D%0Anested%3A%20%7Bx%3A%201%7D&indent=2&style=block&sort_keys=preserve`

---

## What this tool does

Paste **messy, inconsistently-indented, or minified** YAML and get it back
cleanly **reindented and normalized**. Choose how many spaces to indent per
nesting level, alphabetize every mapping's keys, or collapse the whole document
to compact **flow** style. It parses the YAML, so invalid input is reported as
an error instead of being silently mangled.

Everything runs locally in your browser via WebAssembly — your YAML never leaves
your machine and there are no network requests.

## Options

- **Spaces per level** — indentation width for block style, from 1 to 8 (default
  2). YAML forbids tab characters for indentation, so only spaces are offered.
- **Output style**
  - **block** — expanded, multi-line YAML (the usual, readable layout).
  - **flow** — compact, single-line JSON-like YAML (`{name: gizza, tags: [a, b]}`),
    handy for minifying or embedding a value on one line.
- **Key order**
  - **preserve** — keep the original key order (default).
  - **asc** / **desc** — sort every mapping's keys alphabetically, recursively —
    useful for producing diff-stable config files.

## Worked example

Input (inconsistent spacing and inline collections):

```yaml
name:   gizza
tags: [a, b]
nested: {x: 1}
```

Style `block` · Indent `2` spaces · Key order `preserve`

Output:

```yaml
name: gizza
tags:
  - a
  - b
nested:
  x: 1
```

Switch **Output style** to `flow` to get `{name: gizza, tags: [a, b], nested: {x: 1}}`
on a single line, raise **Spaces per level** to `4` to widen the indentation, or
set **Key order** to `asc` to alphabetize the keys.

## Limits and edge cases

- This is a **formatter**, not a linter or schema validator — it checks that the
  YAML *parses* and re-emits it, but it doesn't check your data against a schema.
- **Comments, blank lines, and anchors are not preserved.** The document is parsed
  into a data model and re-emitted, so `# comments`, spacing between blocks, and
  `&anchor`/`*alias` references are dropped — **aliases are expanded** to the value
  they point at.
- Strings that would otherwise be read back as a boolean, null, or number
  (`"true"`, `"null"`, `"123"`) are **double-quoted** to keep their type. Other
  strings are left unquoted where that is unambiguous.
- Indentation is clamped to 1–8 spaces; tabs are not used (YAML forbids tab
  indentation).
- Multi-document streams separated by `---` are supported; each document is
  normalized independently.
- To convert YAML to or from JSON/TOML instead of reformatting it, use a
  dedicated converter — this tool always outputs YAML.

## FAQ

<details>
<summary>Does formatting change my data?</summary>

The **values** are preserved — the document is parsed and re-emitted with the
same data. What changes is the *presentation*: indentation, quoting, key order
(if you sort), and block-vs-flow layout. Comments, blank lines, and anchors are
not carried over, and aliases are expanded to their referenced value.

</details>

<details>
<summary>Can I indent with tabs?</summary>

No. The YAML specification forbids tab characters for indentation, so the tool
only offers spaces (1 to 8 per level). If your editor inserts tabs, this
formatter will replace them with consistent spaces.

</details>

<details>
<summary>What is the difference between block and flow style?</summary>

**Block** style is the familiar expanded, multi-line layout where each key and
list item sits on its own indented line. **Flow** style is compact and
JSON-like, putting mappings in `{ }` and sequences in `[ ]` on a single line —
useful for minifying a document or writing a short value inline.

</details>

<details>
<summary>Why were some of my strings put in quotes?</summary>

A plain YAML scalar like `true`, `null`, or `123` is read back as a boolean,
null, or number. When your data holds those as **strings**, the formatter
double-quotes them (`"true"`, `"123"`) so they keep their string type on the next
parse. Values that are unambiguous are left unquoted.

</details>

<details>
<summary>Why did my YAML fail to format?</summary>

The formatter parses the input first, so anything that isn't valid YAML — an
unterminated flow collection, bad indentation, or a duplicate key — is reported
as an error with the parser's message rather than being guessed at. Fix the
reported issue and try again.

</details>

<details>
<summary>Is my YAML uploaded anywhere?</summary>

No. The formatter runs entirely in your browser via WebAssembly. Your document
never leaves your machine and there are no network requests.

</details>

## Related tools

- [Adjacency Matrix Converter](https://gizza.ai/tools/adjacency-matrix-converter/): Convert a graph between edge list, adjacency matrix, and incidence matrix — directed or undirected, weighted or not. Free, private, runs in your browser.
- [Amazon Order Analyzer](https://gizza.ai/tools/amazon-order-analyzer/): Paste an Amazon order-history CSV export to summarize total spend by month, top items, and category breakdowns. Browser-only, private, with Markdown or JSON output.
- [Avro to JSON Converter](https://gizza.ai/tools/avro-to-json/): Decode Apache Avro Object Container Files (.avro / OCF) to JSON, NDJSON, or the embedded schema — no .avsc needed, free and private in your browser.
- [Reconcile bank statement and ledger CSVs](https://gizza.ai/tools/bank-statement-reconcile/): Match bank-statement CSV rows to ledger rows by date, signed amount, and fuzzy memo similarity, with unmatched and suggested matches.
- [Base64 ⇄ Base64url Converter](https://gizza.ai/tools/base64url-converter/): Convert standard Base64 to URL-safe Base64url and back in your browser — swaps +/ for -_, handles = padding, auto-detects direction. Free, private, no sign-up.
