X Xerobit

YAML to JSON Converter — Validator & Formatter

Convert between YAML 1.2 and JSON with full fidelity. Also validates YAML syntax and formats output. Uses js-yaml under the hood — handles anchors, aliases, multi-line strings, and all standard YAML tags.

YAML to JSON conversion: how it works

This tool uses js-yaml — the most widely-used JavaScript YAML library — to handle the conversion in both directions. When you convert YAML to JSON, the process is: js-yaml parses the YAML text into a JavaScript object tree, then JSON.stringify(tree, null, indent) serializes it to JSON. The conversion is fully in-browser; your data never leaves your machine.

js-yaml implements the YAML 1.2 specification. That matters for edge cases like booleans and octal literals — covered below.

JSON to YAML conversion: the reverse flow

Converting JSON to YAML reverses the pipeline: JSON.parse() reads the JSON string into a JavaScript object, then js-yaml.dump() serializes it to YAML using block style by default. The result is idiomatic, human-readable YAML — nested objects become indented mappings, arrays become block sequences with - prefixes.

One thing that cannot survive the trip: JSON has no concept of anchors, aliases, or comments, so converting JSON to YAML produces clean YAML but with no reuse shortcuts. Round-tripping YAML → JSON → YAML also loses comments and anchor definitions — that is a JSON limitation, not a converter bug.

YAML vs JSON — quick comparison

FeatureYAMLJSON
CommentsYes (#)No
ReadabilityHigh — minimal punctuationMedium — lots of braces and quotes
Whitespace significanceYes — indentation is structureNo — whitespace is cosmetic
Anchors / aliasesYes (&anchor, *alias)No
Multi-line stringsBlock literal (|) and folded (>)Escaped \n only
Parse speedSlower (complex grammar)Very fast (simple grammar)
Browser-native parseNo — needs a libraryYes — JSON.parse()
Primary use caseConfig files, CI/CD, manifestsAPIs, storage, inter-service data

YAML indentation gotchas

YAML's most common source of parse errors is indentation. The rules:

Common YAML types in config files

YAML 1.1 vs YAML 1.2 — what changed

Many tools still implement YAML 1.1 (released 2005). YAML 1.2 (2009) made two breaking changes that catch developers off guard:

Practical examples

A minimal Kubernetes Deployment manifest in YAML, converted to JSON, illustrates how the type system translates. The YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
  labels:
    app: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api

Becomes this JSON:

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

Notice that replicas: 3 becomes the number 3, not the string "3". YAML's type inference carries through to the JSON output.

When anchors and aliases help — and their JSON limitation

Anchors (&name) and aliases (*name) let you define a value once and reference it multiple times. This is the YAML equivalent of a variable or constant. In a GitHub Actions workflow, a common pattern is to define a &node-defaults anchor with the shared node-version and cache settings, then merge it into every job with <<: *node-defaults.

When you convert YAML with anchors to JSON, the aliases are expanded — each reference becomes a full copy of the anchored value. JSON has no reference mechanism, so the DRY structure is lost. If you convert that JSON back to YAML, you get the expanded form with no anchors. Round-tripping collapses anchors irreversibly.

FAQ

Does round-tripping preserve comments?

No. JSON has no comment syntax, so YAML → JSON conversion discards all # comments. Converting that JSON back to YAML produces valid YAML but with no comments. This is a fundamental JSON limitation. If you need comment-preserving YAML transforms, use a tool that works directly in YAML without a JSON intermediate step.

Why did my number turn into a string after conversion?

Likely a YAML 1.1 vs 1.2 conflict. Values like yes, no, on, off are booleans in YAML 1.1 but plain strings in 1.2 — this tool uses 1.2. Also watch for version numbers like 1.10 being parsed as floats: quote them as "1.10" to preserve the string form.

Is this safe for untrusted YAML input?

Yes. This tool uses js-yaml v4's load() function, which is safe-load by default. It refuses to execute arbitrary JavaScript code via custom YAML tags like !!js/function. The old safeLoad() function from v3 was merged into the default behavior in v4, making unsafe loading opt-in rather than opt-out.

Related tools

Related articles

Pillar

Part of Data & Format — JSON, YAML, XML, CSV, SQL, Markdown.


Written by Mian Ali Khalid. Last updated 2026-05-13.