ZonoTools

How to Convert JSON to YAML (Step-by-Step Guide)

By ZonoTools7 min read

Why convert JSON to YAML?

Readability: YAML drops braces and heavy quoting so nested settings read like an outline—easier for humans reviewing Git diffs.

Config use: CI pipelines, Kubernetes manifests, and app configs are often authored as YAML. When your source of truth is JSON (API export, generator output, or fixtures), you convert once at the boundary instead of rewriting by hand.

If you are unsure which format fits where, skim JSON vs YAML: differences and use cases first—this guide focuses on how to convert json to yaml, not choosing a religion.

Method 1: Use an online tool (fastest)

ZonoTools runs JSON to YAML in the browser: paste JSON, copy YAML. Processing stays local—reasonable when payloads include internal hostnames or secrets placeholders you do not want uploaded elsewhere.

Steps: (1) Validate JSON first with JSON Validator if the paste came from logs or Slack. (2) Paste into JSON to YAML. (3) Copy YAML and optionally check with YAML Validator before commit.

For cluster-bound output, pair the result with Kubernetes JSON→YAML examples so you strip server-only fields after converting.

Method 2: Using Python

Install PyYAML (pip install pyyaml), then load JSON and dump YAML with a stable style:

sort_keys=False preserves key order from JSON; default_flow_style=False favors block YAML over inline {}/[] blobs.

python
import json import yaml with open("input.json", encoding="utf-8") as f: data = json.load(f) with open("output.yaml", "w", encoding="utf-8") as f: yaml.safe_dump( data, f, sort_keys=False, default_flow_style=False, allow_unicode=True, )

Method 3: Using CLI tools

yq can read JSON and emit YAML (flags vary slightly by build—Mike Farah’s yq is widely used). jq only emits JSON; pair jq to tidy JSON, then yq to YAML:

Pin tool versions in CI so flags stay stable across laptops and runners.

bash
# JSON → YAML yq -p=json -o=yaml < input.json > output.yaml # Normalize messy JSON first, then convert jq . messy.json | yq -p=json -o=yaml > output.yaml

Common mistakes

Indentation: YAML nesting is whitespace-sensitive—tabs vs spaces or one-column drift rewires the tree. Prefer two spaces, ban tabs in .yamllint or editorconfig, and re-convert from JSON instead of hand-dragging list markers.

Data types: YAML may interpret yes, bare dates, or null variants differently than your JSON consumer expected—quote scalars that must stay strings. Full symptom list and fixes: Common JSON to YAML conversion errors.

Bottom line: pick ZonoTools JSON to YAML when you want zero setup; use Python or yq when conversion must live inside scripts or pipelines—then validate output every time.