YAML configuration for Kubernetes, CI/CD, and application settings is human-readable with indentation and minimal punctuation, but APIs, JavaScript, and JSON Schema tools require strict JSON with quoted keys and brackets. Converting YAML to JSON means translating indented mappings and sequences into quoted objects and arrays while preserving types, anchors, and multi-document streams — without hand-editing quotes and commas for every line. A YAML to JSON converter that validates per YAML 1.2.2 and outputs beautified JSON per RFC 8259 does the translation with line-accurate error reporting and type fidelity.
This expanded A-to-Z guide explains how to convert YAML to JSON online — the YAML vs JSON syntax differences and why YAML is a superset of JSON, step-by-step conversion, handling of anchors and aliases, multi-document streams, type preservation (numbers, booleans, null vs strings), flow vs block styles, real-world Kubernetes and CI/CD examples, validation and common indentation and quoting errors, performance and security considerations (browser-side vs server-side), and tool comparisons — with references to YAML Spec 1.2.2, JSON.org, MDN: JSON, Kubernetes: Configuration, and GitHub Actions Workflow Syntax.
name: Anna\n age: 28\n tags: [vip, eu] or a Kubernetes manifest with apiVersion: v1) into a YAML to JSON converter to get valid JSON ({"name": "Anna", "age": 28, "tags": ["vip", "eu"]}) with beautified 2-space indent and types preserved (numbers stay numbers, booleans stay booleans). The converter handles indentation (spaces, not tabs), anchors (& + *), multi-document streams (---), and type preservation per YAML 1.2.2, with exact line/column for indentation errors.
YAML vs JSON — Same Data, Different Syntax (In Depth)
Both are data serialization formats, but YAML is a superset of JSON per YAML 1.2 — valid JSON is valid YAML, but not vice versa. YAML optimizes for human editing with indentation and comments; JSON optimizes for strict, unambiguous parsing with braces and quotes. The same data serializes to both, but the trade-off is readability vs strictness — YAML is easier to edit but requires consistent indentation, while JSON is verbose but has a single obvious way to parse.
| Aspect | YAML | JSON (RFC 8259) |
|---|---|---|
| Keys | Unquoted if simple (name: Anna), quoted if special ("a: b": 1, "true": 1) | Always double-quoted ("name": "Anna") |
| Structure | Indentation (2 spaces) + - for sequences, flow {a: 1} also allowed | Braces {} + brackets [] + commas |
| Comments | # comment to end of line | Forbidden (use "_comment" key if needed) |
| Types | Unquoted 28 is number, true is bool, null/~ is null, "28" is string | Same, but all strings quoted — "28" is string, 28 is number |
| Anchors | &anchor definition + *alias reuse for DRY | No anchors — must duplicate the value |
| Multi-doc | --- separators for streams (multiple docs in one file) | One value per file (use array [doc1, doc2] for multi) |
Example with nesting, flow sequence, and comment:
# YAML
name: Anna
age: 28
# tags as flow sequence
tags: [vip, eu]
address:
city: Berlin
zip: '10115' # quoted to keep string type (leading zero would be lost as number)
# JSON
{
"name": "Anna",
"age": 28,
"tags": ["vip", "eu"],
"address": {"city": "Berlin", "zip": "10115"}
}
Note the zip code: "10115" in JSON stays '10115' in YAML if quoted to preserve the string type — unquoted 10115 would be parsed as a number and, while the value is the same without leading zeros, quoting signals intent and prevents future "00115" from losing the leading zeros. Type preservation is critical for codes, phone numbers ("+1-555"), and IDs that look numeric but are strings.
Why Convert YAML to JSON? Real-World Use Cases
- Kubernetes:
kubectl get deployment myapp -o jsonyields JSON; converting a YAML manifest to JSON allowsjqfiltering or sending to the API server as JSON per Kubernetes Configuration. Many operators store manifests as YAML in git but convert to JSON for API calls. - GitHub Actions and CI/CD: Workflows are YAML (
.github/workflows/ci.yml) per GitHub Actions Syntax; converting a JSON matrix output (e.g., from a script) to YAML avoids hand-indenting 2-space blocks and ensures the workflow is valid YAML before commit. - Docker Compose:
docker-compose.ymlis YAML; a JSON export from a generator or API becomes YAML for the compose file without hand-formatting. - Configuration management: Tools like Ansible, Helm, and CloudFormation use YAML for human-edited values; JSON from an API or database export (e.g., feature flags) becomes YAML for the values file. Helm's
values.yamlis a prime example. - Documentation and config diff: YAML examples in docs are more readable than JSON for nested config — converting JSON samples to YAML improves docs and code comments.
- JSON Schema validation: YAML configs are often validated via JSON Schema — converting YAML to JSON allows
ajvor similar validators to check the config before deploy.
How to Convert YAML to JSON Online — 3 Steps (With Validation)
- Paste YAML: From a file, Kubernetes manifest (
apiVersion: v1\n kind: Pod), GitHub Actions workflow, or docker-compose.yml. The converter accepts single or multi-document streams separated by---(e.g., two Kubernetes resources in one file with---between). No file upload to a server is needed; the paste is local and can be 100KB+. - Convert with validation: The parser validates per YAML 1.2.2 — indentation (spaces, not tabs, 2 per level is conventional but any consistent indent is valid), anchors (
&def+*def), multi-doc, and types — and outputs beautified JSON with 2-space indent. Numbers, booleans, and null are preserved as typed JSON values, not strings: YAML28→ JSON number 28, YAMLtrue→ JSON true, YAMLnullor~→ JSON null. Quoted "28" stays string. If invalid (e.g., tabs for indentation, inconsistent indent, or unquoteda: b: c), the validator shows the exact line and column with the YAML spec rule and a fix hint. - Copy JSON: The JSON is ready for APIs,
JSON.parse,jq, or JSON Schema validation (ajv). Choose minified for smallest payload or beautified for reading. Multi-document YAML becomes a JSON array of documents:---\n a: 1\n---\n b: 2→[{"a": 1}, {"b": 2}]. Copy as a single JSON array or as separate JSON documents per the tool's option.
Live example: Paste:
name: Anna
age: 28
tags:
- vip
- eu
address:
city: Berlin
zip: '10115'
→ Get:
{
"name": "Anna",
"age": 28,
"tags": ["vip", "eu"],
"address": {"city": "Berlin", "zip": "10115"}
}
Handling YAML Features — Anchors, Multi-Document, Types, and Styles (Extra Info)
- Anchors and aliases (DRY):
defaults: &def {a: 1, b: 2}defines an anchor, andlater: *defis an alias that is expanded to a copy of the anchored node. In JSON, this becomes duplicated:{"defaults": {"a": 1, "b": 2}, "later": {"a": 1, "b": 2}}. Merged keys with<<: *defare also expanded per YAML merge key (though deprecated in YAML 1.2, still common). The converter handles this expansion correctly per the spec, so the JSON is self-contained without anchors. - Multi-document streams: YAML files with
---separators (common in Kubernetes: Deployment + Service in one file) represent multiple documents. JSON has no multi-doc — the converter wraps them as a JSON array:---\n apiVersion: v1\n kind: Pod\n---\n apiVersion: v1\n kind: Service→[{"apiVersion":"v1","kind":"Pod"}, {"apiVersion":"v1","kind":"Service"}]. Choose whether to output as an array or as separate JSON documents. - Types and quoting: Unquoted
28→ JSON number,true/false→ bool,null/~/empty → null,"28"→ string. Special strings containing:,#,-at start, ortrueas a value that should be a string must be quoted in YAML to stay strings — e.g.,"true": 1vstrue: 1(the latter is bool true as key, which becomes JSON{"true": 1}only if quoted). The converter preserves the YAML type, sozip: '10115'stays string "10115" in JSON, not number 10115. - Flow vs block styles: YAML flow style (
{a: 1, b: [1, 2]}) is JSON-like inline; block style (indented) is the default for files. Both parse to the same data; the converter outputs JSON in block (beautified) or flow (minified) per the output option. Input YAML in either style converts correctly. - Tags and explicit types: YAML tags like
!!str 28force string type even for "28" — the converter respects explicit tags and outputs"28"as JSON string, not number. This is rare but important for precise type control.
Common Errors and How to Fix Them
- Tabs instead of spaces: YAML forbids tabs for indentation — "found character '\t' that cannot start any token" → replace tabs with 2 spaces per level. The converter highlights the tab character and suggests the fix.
- Inconsistent indentation: Mixing 2 and 4 spaces in one file — e.g.,
name: Annaat 0,age: 28at 2,city: Berlinat 4 underaddress:at 2 — is flagged as indentation error at the inconsistent line. Use 2 spaces consistently per level. - Unquoted strings with
:or#:a: b: cis parsed as keyawith valueb: cwhereb: cis a mapping, not a string — quote the value:a: "b: c". Similarly,a: foo # bar→ the# baris a comment, soaisfoo, notfoo # bar— quote to keep the#. - Trailing spaces after anchor:
&defwith trailing space → trim; the anchor name is up to the space. - Duplicate keys: YAML allows duplicate keys (last wins, per JSON), but the converter warns — duplicate
name: Annaand latername: Benin the same mapping results in{"name": "Ben"}(last wins), which may be unintended.
The validator pinpoints the exact line and column with the YAML spec rule and a fix hint, so the correction is mechanical, not guesswork. For large files, the error at line 142 is found without scrolling.
Performance and Security Notes
Conversion is O(n) in the size of the input and runs browser-side for privacy — the YAML never leaves the device, unlike server-side converters that log data. This is preferable for configs containing secrets (e.g., password: s3cr3t in a Kubernetes Secret — though secrets should be in .env or secret managers like Vault, not in YAML/JSON). For CI, the offline yq (mikefarah/yq) or python -c "import yaml, json, sys; print(json.dumps(yaml.safe_load(sys.stdin), indent=2))" is equivalent for automation and handles multi-doc with yq -o=json. For multi-MB YAML, streaming with yq is more appropriate than the online 100KB+ chunked parser.
FAQs About Converting YAML to JSON
How do I convert YAML to JSON without code?
Paste YAML (single or multi-document with ---, with anchors) into a YAML to JSON converter to get validated, beautified JSON with types, anchors expanded, and multi-doc as a JSON array. No install, yq, or script needed.
Is YAML a superset of JSON?
Yes per YAML 1.2 — valid JSON is valid YAML, so JSON can be pasted as YAML input and will convert to the same JSON (re-formatted with 2-space indent and without the original JSON's quotes where safe). The reverse is not true due to YAML anchors and comments.
How do I handle anchors in YAML to JSON?
Anchors (&) and aliases (*) are expanded to their values in JSON — the alias is replaced by a deep copy of the anchored node, so the JSON is self-contained. Merge keys (<<: *def) are also expanded.
What about multi-document YAML?
Streams with --- separators become a JSON array of documents: ---\n a: 1\n---\n b: 2 → [{"a": 1}, {"b": 2}]. Choose array or separate documents per the tool's option.
Will my YAML types be preserved in JSON?
Yes — numbers stay numbers, booleans stay booleans, null stays null, and quoted strings stay strings. The converter distinguishes 28 (number) from "28" (string) and preserves '00115' as string "00115" in JSON, not number 115.
How do I handle large YAML files?
The online converter handles 100KB+ with chunked parsing. For multi-MB or multi-doc streams, use yq locally: yq -o=json file.yaml > file.json or yq -o=json '...' file.yaml.
Conclusion
Converting YAML to JSON is indentation and quoting plus type and anchor preservation — YAML's human-friendly indented mappings and sequences become JSON's quoted objects and arrays, with anchors expanded, multi-documents as arrays, and types preserved per YAML 1.2 plain scalar rules. Validating per YAML 1.2 and outputting per RFC 8259 ensures the JSON is correct without hand-editing quotes, commas, and indentation.
Paste the next YAML — single or multi-document, with anchors and comments — to get validated, beautified JSON ready for the API, jq, or JSON Schema validation.