Skip to main content
Data Conversion

Stop Converting JSON to YAML by Hand

You have a Kubernetes manifest in YAML and a CI pipeline that wants JSON. Or the opposite: an API returns JSON and your Ansible inventory speaks YAML. Either way, someone ends up retyping brackets into indentation, and that someone usually makes one mistake that costs a failed deploy.

Hand translation is slow and quiet about its errors. One missing space in YAML, and the whole file parses as a string. One trailing comma in JSON, and the parser refuses everything. The fix is not to get better at retyping. It is to stop retyping.

The paste-transform-verify loop

The workflow that works looks like this:

  • Paste the source config into JSON to YAML or YAML to JSON, depending on the direction.
  • Copy the output straight into your file. No reformatting by hand.
  • If the source was JSON, run it through a formatter first when it arrived minified — one object per line is much easier to diff later.

The verify step matters more than people assume. Converting a manifest is not done when the syntax parses; it is done when the values survived. Quotes are the usual casualty. YAML happily treats version: 1.10 as a number, while JSON would have made it "1.10". If your chart pins a version, a converter that rewrites the type just broke it for you.

Where each format wins

Neither format is better. They are good at different jobs:

  • YAML for files humans maintain: CI pipelines, Docker Compose, inventories. Comments and anchors are worth the whitespace sensitivity.
  • JSON for anything a machine reads back: API payloads, tool configuration, logs. It round-trips without surprises and every language parses it natively.

A practical rule: keep the canonical version in the format the consuming system expects, and convert only when handing files across a boundary. If you find yourself converting the same file every week, that boundary is the actual problem — fix the consumer to accept the source format, and the conversion becomes a one-time migration.

Same trick, other data shapes

The same paste-transform-verify loop covers most data conversion you do in a week. XML configs go through XML to JSON when a legacy endpoint hands you attributes and namespaces nobody asked for. Markdown handbooks turn into HTML with the HTML to Markdown converter when you need the reverse for a docs site that only accepts rich text.

Two habits keep this reliable. First, convert before you edit, never after — editing YAML and JSON versions of the same file in parallel is how the two drift apart. Second, when a converted file misbehaves and the syntax looks fine, check the types first. Strings that became numbers, or booleans that became strings, cause most of the "it parses but it is wrong" bugs.

Set that loop up once and config translation stops being a task. It becomes a two-minute step between the real work.