What a YAML parser returns
A YAML parser turns text into plain data structures: mappings become objects (dictionaries), sequences become arrays (lists) and scalars become strings, numbers, booleans or null. This online YAML parser shows you that result directly. It is the quickest way to answer "what will yaml.safe_load or YAML.parse give me for this file?" without writing a script.
Example: from YAML text to data
defaults: &defaults
adapter: postgres
pool: 5
development:
database: app_dev
settings: *defaults
tags:
- api
- "2024"
Parsing produces:
{
"defaults": { "adapter": "postgres", "pool": 5 },
"development": {
"database": "app_dev",
"settings": { "adapter": "postgres", "pool": 5 }
},
"tags": ["api", "2024"]
}
(The Code tab indents this fully; it is condensed here.) The alias *defaults is replaced by a copy of the anchored mapping, pool is a number and the quoted "2024" stays a string.
How scalars are resolved
| YAML | Parsed as |
|---|---|
true / false | boolean |
yes / no / on | string (YAML 1.2 rules) |
~, null or an empty value | null |
0x1F, 0o14, 1e3 | numbers 31, 12, 1000 |
007 | number 7 |
2024-05-01 | string "2024-05-01" (no date object is created) |
!!str 42 | string "42" (explicit tag) |
These rules explain most "it worked on my machine" YAML bugs. A postal code written as 02134 arrives in your program as the integer 2134, and a list of country codes containing NO is read as the string "NO" here but as the boolean false by a YAML 1.1 library. When the parsed output differs from what you meant, add quotes in the source file rather than patching the value later in code.
Things that do not survive parsing
- Comments are not part of the data model, so they never appear in the parsed output.
- Merge keys (
<<: *base) are not merged by this parser; the aliased mapping shows up under a literal<<key. - Special floats like
.infand.nanparse correctly but are displayed asnull, because JSON cannot represent them. - Key order is kept as written, although YAML itself treats mappings as unordered.
The same parse in your own code
Once the output looks right, you can expect the same structure from the YAML library in your language. Typical calls are:
- JavaScript (js-yaml):
yaml.load(text), oryaml.loadAll(text)for several documents. - Python (PyYAML):
yaml.safe_load(f)andyaml.safe_load_all(f). - Go (gopkg.in/yaml.v3):
yaml.Unmarshal(data, &out). - Ruby (Psych):
YAML.safe_load(text).
Prefer the "safe" loaders in production. They build only plain data types and refuse tags that would create arbitrary objects, which matters whenever the YAML comes from a user or another system. Differences between libraries mostly show up in the edge cases from the table above, so test values such as leading zeros, dates and yes/no with the library you actually deploy.
Multiple documents
A stream such as a combined Kubernetes manifest contains several documents separated by ---. The parser returns them as an array, one element per document, so you can check each resource separately in the tree.
Related tools
To convert the parsed data into a file you can use elsewhere, try the YAML to JSON converter. For a friendlier read-only exploration, open the YAML viewer. JSON input? The JSON parser works the same way.
Privacy
Parsing happens in your browser with JavaScript; your YAML is not uploaded. Only Load URL and Save & Share involve our server.
YAML parser in Python
The standard library has no YAML module, so the usual YAML parser in Python is PyYAML. Call yaml.safe_load() on a string or an open file; plain yaml.load() fails without an explicit Loader argument. What catches people out is typing. PyYAML follows YAML 1.1 rules, so an unquoted yes becomes True and 2024-05-01 becomes a datetime.date object rather than a string.
# pip install pyyaml
import yaml
text = """
name: demo
port: 8080
debug: yes
released: 2024-05-01
tags: [api, "2024"]
"""
data = yaml.safe_load(text)
print(data)
print(type(data["debug"]).__name__, type(data["released"]).__name__)
Paste the same text into the parser above to compare: it applies YAML 1.2 rules and keeps both of those values as strings, so quote them in the file if every reader must agree.