How to Convert JSON to YAML

Converting JSON to YAML makes machine-readable data human-readable. API responses in JSON become cleaner configuration files in YAML. CloudFormation templates shrink significantly. Kubernetes resource definitions become easier to maintain. This guide covers every method to convert JSON to YAML — from a free online tool to production scripts in Python and Node.js.

Convert JSON to YAML Online — Free and Instant

Use Format Pilot’s free online converter for instant JSON-to-YAML conversion. Paste your JSON, select YAML as the output, and click Convert. The result is clean, properly indented YAML that preserves all your data. No signup, no installation, and your JSON never leaves your browser.

JSON to YAML: Side-by-Side Comparison

Input JSON:

{
  "name": "my-service",
  "version": "1.0.0",
  "config": {
    "port": 8080,
    "debug": false,
    "hosts": ["api.example.com", "backup.example.com"]
  }
}

Output YAML:

name: my-service
version: 1.0.0
config:
  port: 8080
  debug: false
  hosts:
    - api.example.com
    - backup.example.com

The YAML version is 35% shorter and significantly easier to read — no quotes on keys, no commas, no nested braces.

Convert JSON to YAML in Python

import json
import yaml

with open('data.json', 'r') as f:
    data = json.load(f)

with open('data.yaml', 'w') as f:
    yaml.dump(data, f, default_flow_style=False, allow_unicode=True, sort_keys=False)

print("JSON converted to YAML successfully.")

The default_flow_style=False parameter ensures block-style YAML output — each key on its own line with proper indentation — rather than inline flow style which looks similar to JSON.

Convert JSON to YAML in Node.js

const yaml = require('js-yaml');
const fs = require('fs');

const jsonData = JSON.parse(fs.readFileSync('data.json', 'utf8'));
const yamlOutput = yaml.dump(jsonData, { indent: 2, lineWidth: -1 });

fs.writeFileSync('data.yaml', yamlOutput);
console.log('Conversion complete.');

JSON to YAML for Kubernetes and CloudFormation

When converting Kubernetes resource definitions or AWS CloudFormation templates from JSON to YAML, the standard conversion process works well. For CloudFormation specifically, intrinsic functions like {"Fn::Sub": "..."} convert to !Sub "..." in YAML shorthand — a feature that makes templates more readable but requires the cfn-flip tool rather than a general JSON-to-YAML converter. For Kubernetes resources, general JSON-to-YAML conversion works perfectly since Kubernetes uses standard YAML without any special tags.

Frequently Asked Questions

Is JSON valid YAML?

Yes — JSON is a subset of YAML 1.2. Any valid JSON document can be parsed by a YAML 1.2 parser without modification. This means you do not always need to convert JSON to YAML to use it in YAML-based systems. However, converting to native YAML format makes configuration files significantly more readable and editable.

Will JSON to YAML conversion change my data values?

No — all values (strings, numbers, booleans, nulls, arrays, objects) are preserved exactly. The conversion only changes the notation format, not the data. The one exception is that certain strings that look like YAML special values (like “true”, “null”, “1e3”) may need to be quoted in YAML to prevent misinterpretation. A good converter handles this automatically.

How do I keep JSON key order when converting to YAML?

In Python, use sort_keys=False in the yaml.dump() call to preserve the original key order from the JSON object. By default, PyYAML sorts keys alphabetically. In Node.js with js-yaml, key order is preserved from the JavaScript object by default.