DevToolBoxKOSTENLOS
Blog

JSON-YAML Konverter Online Guide: Syntax, Tools und Best Practices

14 Min. Lesezeitvon DevToolBox
TL;DR

JSON und YAML sind die beiden dominierenden Datenserialisierungsformate. JSON eignet sich fur APIs, YAML fur Konfigurationsdateien. YAML ist ein Superset von JSON mit Kommentaren, Ankern und mehrzeiligen Strings. Achten Sie auf YAML-Fallstricke wie das Norwegen-Problem.

Kernpunkte
  • JSON verwendet Klammern und strenge Anfuhrungszeichen; YAML verwendet Einruckung mit minimaler Interpunktion.
  • YAML unterstutzt Kommentare (#), Anker/Aliase (&/*) und mehrzeilige Strings (| und >).
  • Das "Norwegen-Problem": unzitiertes NO, YES, on, off werden in YAML 1.1 zu Booleans.
  • In Python immer yaml.safe_load() statt yaml.load() verwenden.
  • Kubernetes, Docker Compose, GitHub Actions verwenden YAML als Hauptformat.
  • Verwenden Sie js-yaml (JavaScript), PyYAML (Python) oder yq (CLI) fur die Konvertierung.

Testen Sie unseren kostenlosen JSON-YAML Konverter →

JSON vs YAML: Syntaxvergleich

JSON und YAML stellen die gleichen Datenstrukturen mit unterschiedlicher Syntax dar.

Die gleichen Daten in beiden Formaten:

JSON-Format
{
  "server": {
    "host": "localhost",
    "port": 8080,
    "ssl": true
  },
  "database": {
    "name": "myapp",
    "replicas": [
      "db1.example.com",
      "db2.example.com"
    ]
  },
  "features": [
    "authentication",
    "logging",
    "rate-limiting"
  ]
}
YAML-Format
# Server configuration
server:
  host: localhost
  port: 8080
  ssl: true

# Database settings
database:
  name: myapp
  replicas:
    - db1.example.com
    - db2.example.com

features:
  - authentication
  - logging
  - rate-limiting

YAML eliminiert Klammern und verwendet Einruckung fur die Verschachtelung.

Wann JSON oder YAML verwenden

Die Wahl hangt davon ab, wer die Datei liest und schreibt:

JSON verwenden fur:
  • REST-APIs und GraphQL-Antworten
  • Datenaustausch zwischen Microservices
  • MongoDB-Datenbanken
  • JavaScript/TypeScript-Projekte
  • Package-Manifeste
YAML verwenden fur:
  • Kubernetes-Manifeste
  • Docker-Compose-Konfiguration
  • CI/CD-Pipelines
  • Ansible-Playbooks
  • Haufig bearbeitete Konfigurationsdateien

YAML-Anker, Aliase und mehrzeilige Strings

YAML-Funktionen ohne JSON-Aquivalent.

Anker und Aliase

Anker (&) definieren wiederverwendbare Blocke, Aliase (*) referenzieren sie:

# Define reusable defaults with an anchor
defaults: &default_db
  adapter: postgres
  host: localhost
  port: 5432
  pool_size: 10

# Reference with alias and override specific fields
development:
  database:
    <<: *default_db          # Merge all defaults
    database: myapp_dev
    pool_size: 5             # Override pool_size

staging:
  database:
    <<: *default_db
    database: myapp_staging
    host: staging-db.internal

production:
  database:
    <<: *default_db
    database: myapp_prod
    host: prod-db.internal
    pool_size: 25

# After JSON conversion (anchors fully expanded):
# {
#   "defaults": { "adapter": "postgres", "host": "localhost", "port": 5432, "pool_size": 10 },
#   "development": {
#     "database": { "adapter": "postgres", "host": "localhost", "port": 5432, "pool_size": 5, "database": "myapp_dev" }
#   },
#   ...
# }

Mehrzeilige Strings

YAML bietet zwei Block-Skalar-Stile:

Literalblock (|): Bewahrt Zeilenumbruche.

Faltblock (>): Verbindet Zeilen mit Leerzeichen.

# Literal block (|) - preserves newlines exactly
script: |
  #!/bin/bash
  echo "Starting deployment..."
  docker compose pull
  docker compose up -d
  echo "Done!"

# Folded block (>) - joins lines with spaces
description: >
  This is a long description
  that spans multiple lines.
  Each newline becomes a space
  in the resulting string.

# Strip trailing newline with |-
sql_query: |-
  SELECT users.name, orders.total
  FROM users
  JOIN orders ON users.id = orders.user_id
  WHERE orders.created_at > '2024-01-01'

# Keep all trailing newlines with |+
message: |+
  Line 1
  Line 2

  (trailing newlines preserved)


# JSON equivalents:
# "script": "#!/bin/bash\necho \"Starting...\n..."
# "description": "This is a long description that spans..."
# "sql_query": "SELECT users.name..."  (no trailing \n)

Chomping-Indikatoren: |+ alle behalten, |- alle entfernen.

Konvertierung in JavaScript (js-yaml)

js-yaml ist der am weitesten verbreitete YAML-Parser fur JavaScript:

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

// ===== JSON to YAML =====
const jsonData = {
  apiVersion: 'apps/v1',
  kind: 'Deployment',
  metadata: { name: 'web-app', labels: { app: 'web' } },
  spec: {
    replicas: 3,
    selector: { matchLabels: { app: 'web' } },
    template: {
      spec: {
        containers: [{
          name: 'app',
          image: 'nginx:1.25',
          ports: [{ containerPort: 80 }]
        }]
      }
    }
  }
};

const yamlOutput = yaml.dump(jsonData, {
  indent: 2,
  lineWidth: 120,
  noRefs: true,       // Don't use YAML anchors
  sortKeys: false,     // Preserve key order
  quotingType: '"',    // Use double quotes
});
console.log(yamlOutput);

// ===== YAML to JSON =====
const yamlString = fs.readFileSync('config.yaml', 'utf8');
const parsed = yaml.load(yamlString);
const jsonString = JSON.stringify(parsed, null, 2);
fs.writeFileSync('config.json', jsonString);

// ===== Handle multi-document YAML =====
const multiDoc = `
---
name: service-a
port: 3000
---
name: service-b
port: 3001
`;
const docs = [];
yaml.loadAll(multiDoc, (doc) => docs.push(doc));
console.log(JSON.stringify(docs, null, 2));
// [{ "name": "service-a", "port": 3000 }, { "name": "service-b", "port": 3001 }]

Fur TypeScript sind Typdefinitionen integriert.

Testen Sie unseren kostenlosen JSON-YAML Konverter →

Konvertierung in Python (PyYAML, ruamel.yaml)

Python hat zwei Haupt-YAML-Bibliotheken.

PyYAML

# pip install pyyaml
import yaml
import json

# ===== YAML to JSON =====
yaml_text = """
server:
  host: localhost
  port: 8080
  features:
    - auth
    - logging
  database:
    name: myapp
    ssl: true
"""

# ALWAYS use safe_load (never yaml.load with untrusted input)
data = yaml.safe_load(yaml_text)
json_output = json.dumps(data, indent=2, ensure_ascii=False)
print(json_output)

# ===== JSON to YAML =====
json_text = '{"name": "app", "version": "2.0", "debug": false}'
data = json.loads(json_text)
yaml_output = yaml.dump(data, default_flow_style=False, allow_unicode=True, sort_keys=False)
print(yaml_output)

# ===== File conversion =====
with open('config.yaml', 'r') as yf:
    config = yaml.safe_load(yf)

with open('config.json', 'w') as jf:
    json.dump(config, jf, indent=2, ensure_ascii=False)

# ===== Multi-document YAML =====
multi_yaml = """
---
name: doc1
value: 100
---
name: doc2
value: 200
"""
docs = list(yaml.safe_load_all(multi_yaml))
print(json.dumps(docs, indent=2))

ruamel.yaml (Kommentare erhalten)

ruamel.yaml erhalt Kommentare beim Bearbeiten:

# pip install ruamel.yaml
from ruamel.yaml import YAML
from io import StringIO
import json

yaml_handler = YAML()
yaml_handler.preserve_quotes = True

# Load YAML with comments preserved
yaml_text = """
# Application configuration
app:
  name: my-service    # Service name
  port: 3000          # Listen port
  debug: false
"""

data = yaml_handler.load(yaml_text)

# Modify a value
data['app']['port'] = 8080

# Write back - comments are preserved!
output = StringIO()
yaml_handler.dump(data, output)
print(output.getvalue())
# Output still has "# Application configuration" and inline comments

# Convert to JSON (comments lost in JSON, but preserved in YAML round-trip)
json_output = json.dumps(dict(data), indent=2, default=str)
print(json_output)

Konvertierung per CLI (yq, jq)

CLI-Tools sind ideal fur schnelle Konvertierungen:

yq: Das YAML-Schweizer-Taschenmesser

yq ist ein leichter YAML-Prozessor:

# Install yq (Mike Farah version)
# macOS: brew install yq
# Linux: snap install yq  OR  wget from GitHub releases
# Windows: choco install yq

# ===== YAML to JSON =====
yq -o=json config.yaml
yq -o=json '.' config.yaml > config.json

# ===== JSON to YAML =====
yq -o=yaml config.json
yq -o=yaml -P '.' config.json > config.yaml  # -P for pretty print

# ===== Query and filter =====
yq '.server.port' config.yaml              # Extract a value
yq '.spec.containers[0].image' deploy.yaml # Array access
yq '.metadata.labels' deploy.yaml          # Get nested object

# ===== Modify in-place =====
yq -i '.server.port = 9090' config.yaml
yq -i '.spec.replicas = 5' deploy.yaml

# ===== Merge multiple files =====
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' base.yaml override.yaml

# ===== Convert multi-document YAML to JSON array =====
yq -o=json -s '.' multi-doc.yaml

jq

jq verarbeitet YAML nicht direkt, kombinierbar mit yq:

# Pipe yq output through jq for advanced JSON processing
yq -o=json config.yaml | jq '.server'
yq -o=json deploy.yaml | jq '.spec.template.spec.containers[] | .name'

# Use jq to transform JSON, then convert to YAML
cat data.json | jq '{filtered: .items | map(select(.active))}' | yq -o=yaml -P

Schnelle Einzeiler

# Python one-liner: YAML to JSON
python3 -c 'import sys,yaml,json; json.dump(yaml.safe_load(sys.stdin),sys.stdout,indent=2)' < config.yaml

# Python one-liner: JSON to YAML
python3 -c 'import sys,yaml,json; print(yaml.dump(json.load(sys.stdin),default_flow_style=False))' < config.json

# Ruby one-liner: YAML to JSON
ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.safe_load(STDIN.read))' < config.yaml

Kubernetes-Manifeste

Kubernetes hat YAML zur Standardsprache der Cloud-Infrastruktur gemacht.

Ein typisches Deployment-Manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-application
  namespace: production
  labels:
    app: web
    version: "2.0"         # Quoted to prevent float interpretation
    environment: production
  annotations:
    description: >-        # Folded block, strip trailing newline
      Production web application deployment
      with auto-scaling and health checks
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: myregistry/web-app:2.0.1
          ports:
            - containerPort: 8080
              protocol: TCP
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: url
            - name: LOG_LEVEL
              value: "info"        # Quoted to ensure string
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 512Mi
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10
---
# Multiple resources in one file
apiVersion: v1
kind: Service
metadata:
  name: web-service
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080
  type: ClusterIP

Kubernetes-YAML-Muster: verschachtelte Mappings, Sequenzen, mehrzeilige Strings.

Docker Compose und CI/CD

Docker Compose und GitHub Actions sind ein weiteres YAML-Okosystem:

Docker Compose

# docker-compose.yml
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        NODE_ENV: production
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/myapp
      - REDIS_URL=redis://cache:6379
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started
    volumes:
      - ./uploads:/app/uploads
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user -d myapp"]
      interval: 10s
      timeout: 5s
      retries: 5

  cache:
    image: redis:7-alpine
    command: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru

volumes:
  postgres_data:

GitHub Actions

GitHub-Actions-Workflows verwenden spezifische YAML-Muster:

# .github/workflows/ci.yml
name: CI Pipeline

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm
      - run: npm ci
      - run: npm test
      - run: npm run build

  deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Deploy to production
        env:
          DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
        run: |
          echo "Deploying to production..."
          ./scripts/deploy.sh

Testen Sie unseren kostenlosen JSON-YAML Konverter →

YAML-Fallstricke

YAMLs Flexibilitat bringt Fallstricke mit sich:

Das Norwegen-Problem

YAMLs beruhmtester Fallstrick:

# YAML 1.1 boolean coercion (PyYAML, many other parsers)
# These ALL become booleans when unquoted:

countries:
  - US       # String "US" (ok)
  - GB       # String "GB" (ok)
  - NO       # BECOMES: false  (Norway disappears!)
  - FR       # String "FR" (ok)

settings:
  verbose: yes    # BECOMES: true  (not the string "yes")
  debug: no       # BECOMES: false
  feature: on     # BECOMES: true
  legacy: off     # BECOMES: false
  confirm: y      # BECOMES: true
  cancel: n       # BECOMES: false

# FIX: Always quote ambiguous values
countries:
  - "US"
  - "GB"
  - "NO"     # Now correctly a string
  - "FR"

settings:
  verbose: "yes"  # String "yes"
  debug: "no"     # String "no"

Landercode NO wird zu false. Losung: Strings zitieren.

Einruckungsfehler

YAML verwendet nur Leerzeichen (keine Tabs). Editor auf 2 Leerzeichen einstellen:

# .editorconfig - enforce consistent YAML formatting
[*.{yml,yaml}]
indent_style = space
indent_size = 2
tab_width = 2
insert_final_newline = true
trim_trailing_whitespace = true

# .yamllint.yml - lint configuration
---
extends: default
rules:
  indentation:
    spaces: 2
    indent-sequences: true
  truthy:
    check-keys: true
    allowed-values: ["true", "false"]
  line-length:
    max: 120

Unerwartete Booleans

Versionsnummern 1.0 werden zu Fliesskommazahlen:

# More unexpected type coercions in YAML:

version: 1.0        # BECOMES: float 1.0 (not string "1.0")
version: "1.0"      # String "1.0" (correct)

octal: 0o17         # BECOMES: integer 15
hex: 0xFF           # BECOMES: integer 255

date: 2024-01-15    # BECOMES: date object (in some parsers)
date: "2024-01-15"  # String "2024-01-15" (correct)

null_trap: null      # BECOMES: null (not string "null")
null_trap: ~         # ALSO BECOMES: null
null_trap: ""        # Empty string (if you want empty, not null)

# Special float values
infinity: .inf       # BECOMES: Infinity
not_a_number: .nan   # BECOMES: NaN

# Rule of thumb: if it's not obviously a string, quote it

YAML-Sicherheit

YAML-Parser konnen gefahrlich sein.

Gefahrlich: yaml.load(data) kann beliebige Objekte instanziieren.

# DANGEROUS - Never do this with untrusted YAML input!
import yaml

# This YAML payload can execute arbitrary commands:
malicious_yaml = """
!!python/object/apply:os.system
  args: ['echo HACKED > /tmp/pwned']
"""

# BAD: yaml.load() with FullLoader allows object construction
# data = yaml.load(malicious_yaml, Loader=yaml.FullLoader)  # DANGER!

# GOOD: safe_load() only allows basic types
data = yaml.safe_load(malicious_yaml)  # Raises ConstructorError

# ALSO GOOD: ruamel.yaml with safe type
from ruamel.yaml import YAML
safe_yaml = YAML(typ='safe')
data = safe_yaml.load(malicious_yaml)  # Raises error

Sicher: yaml.safe_load() verwenden.

js-yaml v4 ist standardmassig sicher.

Eingabegrosse begrenzen, Schema validieren, sicheres Laden erzwingen.

Vergleichstabelle

Umfassender Vergleich von JSON und YAML:

FeatureJSONYAML
SyntaxBraces {} and brackets []Indentation-based
CommentsNot supportedSupported with #
String QuotingRequired (double quotes)Optional for most strings
Multi-line StringsEscape with \nBlock scalars: | and >
Anchors / AliasesNot supportedSupported with & and *
Multiple DocumentsOne per fileYes, separated by ---
Data TypesString, Number, Boolean, null, Array, ObjectAll JSON types + dates, binary, custom tags
Parsing SpeedFast (simple grammar)Slower (indentation-sensitive)
File SizeLarger (quotes, braces)Smaller (minimal punctuation)
ToolingUniversal (every language)Good (PyYAML, js-yaml, yq)
Primary UseAPIs, data exchangeConfiguration files
Superset RelationBase formatSuperset of JSON

Haufig gestellte Fragen

Was ist der Unterschied zwischen JSON und YAML?

JSON verwendet Klammern, YAML Einruckung. YAML unterstutzt Kommentare und mehrzeilige Strings.

Wie konvertiere ich JSON online zu YAML?

JSON in ein Online-Tool einfugen fur automatische Konvertierung.

Ist YAML besser als JSON fur Konfiguration?

Ja, fur menschlich bearbeitete Dateien.

Was ist das Norwegen-Problem?

YAML 1.1 interpretiert NO als false. Losung: Anfuhrungszeichen.

Warum yaml.safe_load() verwenden?

yaml.load() kann beliebigen Code ausfuhren.

Werden YAML-Kommentare erhalten?

Nein. Verwenden Sie ruamel.yaml.

Wie konvertiere ich per CLI?

yq -o=json file.yaml verwenden.

Welche YAML-Funktionen gehen verloren?

Kommentare, Anker, mehrzeilige Blocke, Mehrfachdokumente, Datumstypen.

Das Verstandnis der Beziehung zwischen JSON und YAML ist fur moderne Softwareentwicklung unerlasslich.

Konvertieren Sie JSON und YAML sofort mit unserem kostenlosen Tool. →

𝕏 Twitterin LinkedIn
War das hilfreich?

Bleiben Sie informiert

Wöchentliche Dev-Tipps und neue Tools.

Kein Spam. Jederzeit abbestellbar.

Verwandte Tools ausprobieren

Y{}JSON ↔ YAML Converter{ }JSON FormatterYMLYAML Validator & Formatter🐳Docker Compose Generator

Verwandte Artikel

YAML zu JSON Konverter: Vollstandiger Leitfaden mit Code-Beispielen

Kostenloser Online YAML-zu-JSON-Konverter. Lernen Sie YAML-Syntax und konvertieren Sie zwischen YAML und JSON mit Code-Beispielen in JavaScript, Python, Go und Bash.

JSON vs YAML vs TOML: Welches Config-Format sollten Sie verwenden?

Vergleichen Sie JSON, YAML und TOML Konfigurationsformate.

YAML Syntax & Validierung: HÀufige Fehler und Lösungen

YAML-Syntax meistern: EinrĂŒckungsregeln, Parser-Fehler, Datentypen und Best Practices.

YAML Anker, Aliase & Merge Keys

YAML DRY-Prinzipien mit Ankern (&), Aliasen (*) und Merge Keys (<<) meistern. Duplizierung in Docker Compose und CI/CD reduzieren.