🛠️DevTools

How to Convert JSON to YAML

YAML is often preferred over JSON for configuration files due to its readability. This guide covers converting JSON to YAML for DevOps tools like Kubernetes and Docker. For JSON basics, see our [JSON Guide](/guides/json).

Try JSON to YAML Converter Tool →

JSON vs YAML: Key Differences

FeatureJSONYAML
SyntaxBrackets & bracesIndentation-based
ReadabilityGoodExcellent
CommentsNot allowedSupported with #
Data typesBasic typesMore types (dates, etc.)
File sizeSlightly largerMore compact
Use casesAPIs, data exchangeConfiguration files

When to use YAML:

  • Kubernetes manifests
  • Docker Compose files
  • CI/CD pipelines (GitHub Actions, GitLab CI)
  • Ansible playbooks
  • Human-edited configuration

Convert JSON to YAML in JavaScript

Use the js-yaml library for reliable conversion:

javascript
// npm install js-yaml
const yaml = require('js-yaml');

const jsonData = {
  apiVersion: "v1",
  kind: "Pod",
  metadata: {
    name: "my-pod",
    labels: {
      app: "web"
    }
  },
  spec: {
    containers: [
      {
        name: "nginx",
        image: "nginx:latest",
        ports: [{ containerPort: 80 }]
      }
    ]
  }
};

// Convert to YAML
const yamlString = yaml.dump(jsonData, {
  indent: 2,
  lineWidth: -1,  // Don't wrap long lines
  noRefs: true,   // Don't use YAML references
  sortKeys: false // Keep key order
});

console.log(yamlString);
/*
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: web
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - containerPort: 80
*/

// Convert JSON string to YAML
function jsonToYaml(jsonString) {
  const data = JSON.parse(jsonString);
  return yaml.dump(data);
}

Convert JSON to YAML in Python

Python's PyYAML library handles the conversion:

python
# pip install pyyaml
import yaml
import json

json_data = {
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "my-service"
    },
    "spec": {
        "selector": {
            "app": "web"
        },
        "ports": [
            {"port": 80, "targetPort": 8080}
        ]
    }
}

# Convert to YAML
yaml_string = yaml.dump(json_data, 
    default_flow_style=False,  # Use block style
    sort_keys=False,           # Preserve key order
    allow_unicode=True
)
print(yaml_string)

# Convert JSON file to YAML file
def json_to_yaml_file(json_path, yaml_path):
    with open(json_path, 'r') as f:
        data = json.load(f)
    with open(yaml_path, 'w') as f:
        yaml.dump(data, f, default_flow_style=False)

# Convert JSON string to YAML
def json_to_yaml(json_string):
    data = json.loads(json_string)
    return yaml.dump(data, default_flow_style=False)

Command Line Conversion

Convert JSON to YAML using CLI tools:

bash
# Using yq (install: brew install yq)
yq -P input.json > output.yaml

# Using Python one-liner
python -c "import yaml,json,sys; print(yaml.dump(json.load(sys.stdin), default_flow_style=False))" < input.json > output.yaml

# Using Node.js with js-yaml
npx js-yaml input.json > output.yaml

# Convert all JSON files in directory to YAML
for f in *.json; do
  yq -P "$f" > "${f%.json}.yaml"
done

# Using jq + yq together
cat input.json | yq -P

Kubernetes JSON to YAML

Converting Kubernetes manifests from JSON to YAML:

javascript
// Kubernetes Deployment JSON
const deployment = {
  apiVersion: "apps/v1",
  kind: "Deployment",
  metadata: {
    name: "web-app",
    labels: { app: "web" }
  },
  spec: {
    replicas: 3,
    selector: {
      matchLabels: { app: "web" }
    },
    template: {
      metadata: {
        labels: { app: "web" }
      },
      spec: {
        containers: [{
          name: "web",
          image: "nginx:1.21",
          ports: [{ containerPort: 80 }],
          resources: {
            limits: { cpu: "500m", memory: "128Mi" },
            requests: { cpu: "250m", memory: "64Mi" }
          }
        }]
      }
    }
  }
};

// Using kubectl to convert
// kubectl get deployment my-app -o yaml > deployment.yaml

// Or use our JSON to YAML tool online!

Frequently Asked Questions

Why use YAML instead of JSON for Kubernetes?

YAML is more readable, supports comments for documentation, and is the standard format for Kubernetes manifests. Most K8s examples and tools use YAML.

Is YAML a superset of JSON?

Yes! Valid JSON is also valid YAML. YAML parsers can read JSON files directly, but YAML has additional features like comments and anchors.

How do I preserve key order when converting?

Use sortKeys: false in js-yaml or sort_keys=False in PyYAML to maintain the original key order from JSON.

Can I convert YAML back to JSON?

Yes! Use our YAML to JSON converter or libraries like js-yaml (yaml.load) and PyYAML (yaml.safe_load).

Try the JSON to YAML Converter Tool

Now that you understand how it works, try encoding and decoding with our free online tool.

Open JSON to YAML Converter

Last updated: 2026-03-15