DevPad
← Back to blog
·json, yaml, configuration, developer-tools

JSON to YAML Converter: When and How to Convert

Understand the differences between JSON and YAML, when to use each format, and how to convert between them with practical examples.

JSON and YAML are the two most popular data serialization formats. While JSON dominates APIs, YAML excels in configuration files. Understanding when to use each — and how to convert between them — is a practical developer skill.

JSON vs YAML: Key Differences

| Feature | JSON | YAML | |---------|------|------| | Readability | Moderate | High | | Comments | Not supported | Supported (#) | | File extension | .json | .yaml or .yml | | Data types | Strings, numbers, booleans, null, arrays, objects | All JSON types + dates, anchors, references | | Verbose | More | Less | | Parse errors | Common (strict syntax) | Rare (indentation-based) |

When to Use JSON

  • APIs and web services — JSON is the universal API format
  • Data interchange — when sending data between systems
  • JavaScript projects — native to JavaScript
  • When comments aren't needed — pure data representation

When to Use YAML

  • Configuration files — Docker Compose, Kubernetes, CI/CD pipelines
  • When comments are needed — explaining configuration sections
  • Human-edited files — easier to read and edit manually
  • When you want less noise — YAML is more compact for the same data

Side-by-Side Comparison

Simple Object

JSON:

{
  "name": "DevPad",
  "version": "1.0.0",
  "description": "Developer tools"
}

YAML:

name: DevPad
version: "1.0.0"
description: Developer tools

Nested Object

JSON:

{
  "server": {
    "host": "localhost",
    "port": 3000,
    "ssl": true
  }
}

YAML:

server:
  host: localhost
  port: 3000
  ssl: true

Array

JSON:

{
  "tools": ["json-formatter", "jwt-decoder", "base64"]
}

YAML:

tools:
  - json-formatter
  - jwt-decoder
  - base64

With Comments

JSON: (not possible)

{
  "server": {
    "port": 3000
  }
}

YAML:

server:
  # Server configuration
  port: 3000

Convert JSON to YAML

JavaScript

import yaml from 'js-yaml';

const json = { name: "DevPad", version: "1.0.0" };
const yamlStr = yaml.dump(json);
console.log(yamlStr);
// name: DevPad
// version: '1.0.0'

Python

import yaml, json

data = {"name": "DevPad", "version": "1.0.0"}
yaml_str = yaml.dump(data)
print(yaml_str)

Command Line

# Using yq (like jq for YAML)
echo '{"name": "DevPad"}' | yq -P

# Using Python
python3 -c "import yaml, json; print(yaml.dump(json.load(open('data.json'))))"

Convert YAML to JSON

JavaScript

import yaml from 'js-yaml';

const yamlStr = 'name: DevPad\nversion: "1.0.0"';
const data = yaml.load(yamlStr);
const jsonStr = JSON.stringify(data, null, 2);
console.log(jsonStr);

Python

import yaml, json

data = yaml.safe_load("name: DevPad\nversion: '1.0.0'")
json_str = json.dumps(data, indent=2)
print(json_str)

YAML Gotchas

  1. Colon spacingkey:value is wrong, key: value is correct

  2. Unquoted strings — YAML auto-converts some values:

    port: 3000    # integer
    version: "1.0.0"  # string (quoted to prevent float conversion)
    
  3. Special characters — colons, brackets, and hyphens need quotes:

    url: "https://example.com"
    
  4. Multi-line strings — use | for literal, > for folded:

    description: |
      This is a
      multi-line string
    

Best Practices

  1. Use YAML for config, JSON for APIs — follow convention
  2. Always quote version numbers — prevent YAML from converting 1.0 to float
  3. Validate before converting — malformed input causes silent data loss
  4. Use safe_load in Pythonyaml.load() with arbitrary constructors is a security risk
  5. Keep a converter handy — for quick format migration

Try It Now

Use our free JSON↔YAML Converter to convert between formats instantly — with syntax highlighting and error detection.