How to convert YAML to JSON
To convert yaml to json, the tool parses your YAML into data, resolves anchors, aliases and merge keys, and writes the data as JSON. Comments are dropped, because JSON has no way to hold them.
The built-in example is a Compose style file in which two services share settings through an anchor. The result has 15 keys over 29 lines. The anchored settings are copied into both services, so the JSON has no << keys and no aliases left:
"web": {
"restart": "unless-stopped",
"environment": [
"LOG_LEVEL=info"
],
"image": "nginx:1.25",
"ports": [
"80:80"
]
},Convert YAML to JSON online with anchors and merge keys
An anchor, written &name, marks a block you want to reuse. An alias, written *name, inserts a copy. A merge key, <<: *name, copies the keys of the anchored map into the current map, and any key you write yourself overrides the copy. JSON has none of these, so the converter expands them.
Resolve merge keys is on by default. Turn it off to keep a literal "<<" key in the output. An alias that points to an anchor that does not exist is reported as an error, with the name of the missing anchor.
YAML to JSON for files with several documents
A YAML file can hold several documents separated by ---. Kubernetes files often do. JSON holds one value, so when the file has more than one document the result is an array with one item for each, and a note tells you how many were found. Turn on First document only to convert just the first one.
YAML 1.1 and 1.2: yes, no, on and off
The two YAML versions disagree about plain words. With YAML 1.2, the default, yes, no, on, off, y and n are ordinary text. With YAML 1.1 they are true and false. Dates differ too: in 1.1, 2020-01-02 becomes a timestamp and is written in JSON as 2020-01-02T00:00:00.000Z, while in 1.2 it stays as the text you wrote.
Pick the version of the program that will read your file. Ansible, which uses PyYAML, follows 1.1. Many newer libraries follow 1.2. When in doubt, quote the value in the YAML file and both versions agree.
YAML errors with line and column
- Tabs are not allowed for indentation. Use spaces. The message points at the line with the tab.
- Items at the same level must line up exactly, or you get an indentation error.
- The same key twice in one block is reported, because most parsers keep only one of them. For files over 150,000 characters this check is skipped, and a note says so.
- A colon after a key must be followed by a space. A text value that contains a colon followed by a space needs quotes.
- An alias such as
*nameneeds an earlier&name.
What JSON cannot hold
When your YAML uses features outside JSON, a note under the result says what happened:
- Custom tags such as
!Refor!Subfrom CloudFormation are ignored and the value is kept as plain data. The note lists the tags it saw. - Values like
.nanand.infbecome null, because JSON has no way to write them. - Whole numbers larger than 9,007,199,254,740,991 may be rounded.
- Keys that are not text, such as
1,trueornull, become text keys. - Binary values become Base64 text, and sets become lists.
YAML to JSON on the command line and in Python
yq -o=json config.yaml > config.jsonimport json, sys, yaml
print(json.dumps(yaml.safe_load(sys.stdin), indent=2))To go the other way, use JSON to YAML. If your file is TOML instead, TOML to JSON reads it. If the JSON you need is stored as a string with backslashes, JSON Unescape restores it. Spreadsheet data is covered by Excel to JSON, and JSONL Viewer reads files with one JSON value per line.
Limits
- Comments are not kept, and neither are the anchors themselves. The data they point to is.
- Keys that look like whole numbers are moved to the front in numeric order. That comes from how JavaScript stores object keys.
- Very large files take a few seconds, because the whole file is parsed in your browser.
Your YAML is converted in your browser. It is not uploaded or stored. See the Privacy Policy for details.