I was debugging a Kubernetes deployment file at 11 PM. The error was cryptic — something about an indentation issue in a ConfigMap. I'd written the YAML by hand, and somewhere between spec.template.spec.containers[0].env[2].valueFrom.configMapKeyRef.name and the 14 spaces of indentation, I'd made a mistake. At that point, I did what any sensible developer would do: I wrote the config as JSON first, converted it to YAML, and went to bed.

Why You'd Want to Convert JSON to YAML

JSON and YAML are both data serialization formats, but they're optimized for different use cases.

JSON is machine-friendly. It's strict, unambiguous, and every parser in every language handles it identically. YAML is human-friendly. It's built around readability, with indentation instead of brackets, and support for comments.

The sweet spot for converting JSON to YAML is when you're moving from a machine-generated config to a human-maintained one. Here are the three scenarios I hit most often:

Kubernetes manifests: Your CI pipeline generates a JSON representation of a deployment, but you want to review it as YAML before applying it.

CI/CD configuration: GitHub Actions, GitLab CI, and CircleCI all use YAML. If your build tool outputs JSON artifacts, you'll need to convert them.

Ansible playbooks: Ansible uses YAML for everything. If you're importing variables from an external JSON source (like a cloud provider API), you'll convert it to YAML format.

Syntax Differences That Actually Matter

Let me show you the same data in both formats:

{
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "name": "web-app",
    "labels": {
      "app": "web",
      "environment": "production"
    }
  },
  "spec": {
    "replicas": 3,
    "selector": {
      "matchLabels": {
        "app": "web"
      }
    }
  }
}

And the YAML equivalent:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
  labels:
    app: web
    environment: production
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web

YAML is roughly 30% shorter here, with no curly braces, no commas, no quotes (for simple values). The indentation replaces bracket nesting. But here's the thing — YAML's readability comes at a cost.

Indentation is fragile. One extra space and your file is broken. Tab characters? YAML doesn't allow them. This is why using a converter to generate YAML from a well-structured JSON is safer than hand-typing it.

For more on converting between data representations, check out our guide on /blog/jsonformatter--convert-json-to-typescript-interfaces for turning JSON into typed interfaces.

What a Good JSON-to-YAML Converter Should Handle

When I use DevFormatters JSON formatter to convert JSON to YAML, I watch for these specific behaviors:

Multi-line strings

{
  "description": "This is a very long description\nthat spans multiple lines\nin the original JSON."
}

Should become:

description: |
  This is a very long description
  that spans multiple lines
  in the original JSON.

Note the | block scalar indicator. That's YAML's way of preserving newlines. Without it, the converter loses the line breaks.

Numbers and booleans

{
  "count": 42,
  "enabled": true,
  "rate": 0.85
}

Should become:

count: 42
enabled: true
rate: 0.85

No quotes around values. YAML infers types from the value. But there's a gotcha — yes, no, on, off are also boolean values in YAML. If you have a string field that happens to be "yes", a bad converter might output it without quotes, and YAML will interpret it as true. A good converter quotes ambiguous strings.

Null values

{
  "deletedAt": null,
  "notes": null
}

Should become:

deletedAt: null
notes: null

Or alternatively:

deletedAt: ~
notes: ~

Both null and ~ are valid in YAML. Most converters use null for clarity.

If you're working with Go backends that consume YAML configs, our guide on /blog/jsonformatter--json-to-go-struct-online shows how to generate Go types from JSON payloads.

Real Example: Converting a CI/CD Config

Here's a real scenario from last week. Our build system outputs a JSON artifact manifest:

{
  "artifacts": [
    {
      "name": "backend-service",
      "path": "./dist/backend",
      "docker_tag": "v1.2.3",
      "ports": [8080, 8443],
      "health_check": {
        "path": "/health",
        "interval_seconds": 30
      }
    },
    {
      "name": "frontend-app",
      "path": "./dist/frontend",
      "docker_tag": "v1.2.3",
      "ports": [3000],
      "health_check": null
    }
  ]
}

Converting this to YAML gives me:

artifacts:
  - name: backend-service
    path: ./dist/backend
    docker_tag: v1.2.3
    ports:
      - 8080
      - 8443
    health_check:
      path: /health
      interval_seconds: 30
  - name: frontend-app
    path: ./dist/frontend
    docker_tag: v1.2.3
    ports:
      - 3000
    health_check: null

The array-of-objects pattern becomes a YAML list with - markers. The health_check: null preserves the fact that the frontend doesn't have a health check configured. This YAML is immediately usable in a CI/CD pipeline.

For flattening these structures into analyzable data, see /blog/jsonformatter--convert-json-to-csv-without-code.

When NOT to Convert

Not every JSON file should become YAML. Here's when I skip the conversion:

Machine-to-machine communication: If both the producer and consumer are programs, stick with JSON. You don't need human readability.

High-throughput APIs: YAML parsing is slower than JSON parsing. For performance-critical paths, JSON wins.

Dynamic or deeply nested data: YAML's indentation gets hard to read past 4–5 levels of nesting. JSON's bracket matching is more forgiving.

One-liner configs: If your config is 5 lines, just write YAML directly. No need to convert.

FAQ

Q: Is JSON to YAML conversion lossless?

A: Almost. Comments are lost because JSON doesn't support them. Certain YAML-specific features like anchors and aliases can't be represented in JSON.

Q: Can I convert YAML back to JSON?

A: Yes. YAML is a superset of JSON — valid JSON is valid YAML. Converting YAML to JSON is straightforward but you lose comments and multi-line formatting.

Q: Does the converter handle Kubernetes manifests correctly?

A: Yes, as long as the JSON input is valid. The YAML output preserves field names and structure exactly.

Q: What happens to JSON arrays in YAML?

A: Arrays become YAML lists using the - syntax. Each element is indented under the list key.

Q: How does YAML handle multi-line strings from JSON?

A: Proper converters use YAML block scalars (| or >) for strings containing newlines.

Q: Can I preserve comments when converting JSON to YAML?

A: No. JSON doesn't support comments, so they can't be preserved. Add comments manually after conversion.

Q: Is there a size limit for JSON-to-YAML conversion?

A: Online tools typically handle files up to a few MB. For large files, use a command-line tool like yq or python3 -c "import json, yaml; ...".

Q: Why would I convert JSON to YAML instead of writing YAML directly?

A: Converting from JSON eliminates indentation errors, handles complex nesting automatically, and is faster when working with machine-generated configs.


Ready to convert your JSON to YAML — or any other format? Try the DevFormatters JSON formatter for instant conversions between JSON, YAML, TypeScript, Go, Java, Python, CSV, and XML.