JSON to YAML One Liner

Tung Nguyen
BoltOps

--

I don’t remember exactly where I got these one-liners from anymore. It’s been in my expander for a while. Here are useful oneliners to convert JSON to YAML and vice versa.

JSON to YAML (stdout):

ruby -ryaml -rjson -e 'puts YAML.dump(JSON.load(ARGF))' < example.json

JSON to YAML (redirect to file):

ruby -ryaml -rjson -e 'puts YAML.dump(JSON.load(ARGF))' < example.json > example.yml

YAML to JSON (stdout):

ruby -ryaml -rjson -e 'puts YAML.dump(JSON.load(ARGF))' < example.yml

YAML to JSON (redirect to file):

ruby -ryaml -rjson -e 'puts YAML.dump(JSON.load(ARGF))' < example.yml > example.json

Hope you found this helpful! 🎉

Thanks for reading this far. If you found this post useful, I’d really appreciate it if you recommend this post (by clicking the clap button) so others can find it too! Also, follow me on Medium.

P.S. Be sure to join the BoltOps newsletter to receive free DevOps tips and updates.

Originally published at blog.boltops.com on September 16, 2017.

--

--