To convert JSON to YAML, parse the JSON and then dump the object as YAML: JSON.parse() and yaml.dump() with js-yaml in Node, json.load() and yaml.dump() with PyYAML in Python, or yq -o=yaml input.json with yq. Calling yaml.load() on JSON only parses it back into an object, so nothing is converted.
Only the PyYAML conversion was run, followed by a round-trip check; the js-yaml and yq commands are cited from their own documentation.
Table of Contents
Why yaml.load() alone does not convert JSON to YAML
// This is what the page shipped:
const doc = yaml.load(fs.readFileSync('/path/to/input.json', 'utf8'));
console.log(doc); // <- logs a parsed JS OBJECT, not YAML text
yaml.load() takes YAML (or JSON, since it's valid YAML) and returns a JavaScript object, the reverse direction. That is why the snippet above runs without an error and converts nothing: YAML 1.2 is the revision whose "primary focus was making YAML a strict superset of JSON" (YAML 1.2.2 specification, section 1.2), so a YAML 1.2 parser such as js-yaml reads the JSON as it is. Converting to YAML text means yaml.dump(), which that snippet never called:
const yaml = require('js-yaml');
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('input.json', 'utf8'));
const yamlText = yaml.dump(data);
console.log(yamlText);
fs.writeFileSync('output.yaml', yamlText);
That corrected snippet is cited, not executed. The fix itself needs no citation: it's the same yaml.load/yaml.dump API pattern documented in js-yaml's own README, applied in the direction the task actually needs.
Converting JSON to YAML with PyYAML
Node has no YAML support built in (JSON.parse is standard-library, YAML is not, in effectively every language). With PyYAML installed, Python does the same conversion; here's a real run, same logic as the corrected JS above:
$ python3 -c "
import json, yaml
with open('sample_input.json') as f:
data = json.load(f)
out = yaml.dump(data, default_flow_style=False, sort_keys=False)
print(out)
"
name: John Doe
age: 30
active: true
balance: 19.99
tags: null
skills:
- Python
- DevOps
address:
city: Austin
zip: '78701'
Input was {"name": "John Doe", "age": 30, "active": true, "balance": 19.99, "tags": null, "skills": ["Python", "DevOps"], "address": {"city": "Austin", "zip": "78701"}}. Note zip: '78701': PyYAML quoted it because an unquoted 78701 would parse back as an integer, which the source JSON string wasn't. That's the dumper protecting you, not a bug. If you run how-to-convert-json-to-yaml.sh, it converts this same sample with PyYAML, confirms that zip comes out quoted, parses the YAML back and compares it with the original, then reports whether js-yaml and yq are installed without installing either.
Round-tripping is not automatically lossless, but a real library protects you
Parsing that YAML back and comparing to the original JSON:
$ python3 -c "... yaml.safe_load(open('converted.yaml')) == json.load(open('sample_input.json')) ..."
round-trip equal to original: True
That held because yaml.dump() quotes any value that would otherwise be ambiguous: a JSON string "on" comes back out as 'on', not the bare word, which is what keeps it a string instead of a YAML 1.1 boolean on the way back in. The risk isn't the conversion library; it's a human editing the generated .yaml afterward and removing a quote that looked unnecessary. The YAML syntax breakdown has the measured evidence for how badly that goes, including a plain 22:22 parsing as two different integers in two different libraries, and isn't repeated here.
Can jq or yq convert JSON to YAML?
jq 1.7.1 reads and writes JSON only; there's no YAML output mode, verified by running it against the sample file above. yq (the tool that adds YAML support in the same style) was not run and nothing on this page cites a run of it; if you have it, yq -o=yaml input.json converts, per yq's own documentation. Check which yq you have before you copy that: two unrelated programs use the name, and the one Ubuntu's archive installs is not the Go binary most tutorials assume: different expression language, different flags.
For the difference between the two formats this conversion moves you between, see the YAML vs JSON comparison, including a measured parse-speed benchmark.