How YAML and JSON relate
YAML and JSON describe the same kinds of data: maps of keys to values, ordered lists, strings, numbers, booleans, and null. JSON uses braces, brackets, and quotes, which makes it strict and easy for machines to parse. YAML uses indentation and minimal punctuation, which makes it easier for people to write, and it is the format behind Kubernetes manifests, Docker Compose files, GitHub Actions workflows, Ansible playbooks, and OpenAPI specifications.
Since YAML 1.2, every valid JSON document is also valid YAML. Converting JSON to YAML is therefore always possible. Converting YAML to JSON is possible for the data itself, but YAML features such as comments and anchors have no JSON equivalent.
Indentation and structure
In YAML, indentation defines structure. Nested keys must be indented consistently, and tabs are not allowed for indentation at all, only spaces. A single misaligned line can silently move a key to a different parent instead of causing an error, which is a common source of broken deployment configuration.
List items start with a dash followed by a space. When a list contains maps, the keys of each item must line up under the first key after the dash. Converting a suspicious YAML file to JSON is a quick way to check its real structure, because the braces and brackets in the JSON output show exactly where each value ended up.
Implicit typing pitfalls
YAML guesses types for unquoted values, and the guesses are not always what you meant. In YAML 1.1, which many tools still use, the words yes, no, on, and off become booleans. The country code NO for Norway becoming false is so common that it is known as the Norway problem.
Numbers cause similar surprises. A version string such as 1.10 becomes the number 1.1, a value like 0755 may be read as an octal number, and long numeric IDs can lose precision. Times written like 12:30 were treated as base-60 numbers in YAML 1.1. The safe rule is simple: quote any value that must stay a string, especially version numbers, postal codes, IDs, and country codes. After converting to JSON, check that strings are still wrapped in quotes.
Multi-line strings, anchors, and comments
YAML has two block styles for long text. The literal style, introduced with a pipe character, keeps line breaks exactly as written, which suits scripts and certificates. The folded style, introduced with a greater-than sign, joins lines with spaces, which suits long descriptions. In JSON both become ordinary strings, with line breaks written as \n.
Anchors (&name) and aliases (*name) let YAML reuse a block of data in several places, and the merge key << combines maps. When converting to JSON, the referenced data is expanded and copied into every location, so the JSON output can be noticeably larger than the source.
Comments starting with # are discarded during conversion, because JSON has no comment syntax. If the comments document important decisions, keep the YAML file as the source of truth and treat the JSON as generated output.
A checklist before you convert
Before converting a YAML file, check that it uses spaces rather than tabs and that every nested block is indented consistently. Quote values that must remain strings, such as versions, IDs, and codes. If the file contains several documents separated by --- lines, decide whether you need each document separately or an array of all of them, because a single JSON document cannot hold multiple YAML documents directly.
After converting, read the JSON output critically. Booleans where you expected text, numbers that lost digits, and keys appearing under an unexpected parent are the classic signs of an indentation or typing problem in the source YAML.