07 March 2021

Convert JSON to YAML from the command line


I was looking for a one-liner to convert between JSON and YAML in the shell and I came across yq. In short it’s…

# yml -> json
yq eval -j sample.yml

# json -> yml
yq eval -P sample.json

The flags are -j for --tojson and -P for --prettyPrint.

There are various ways to install it including on mac: brew install yq

It supports the convention of using - as the filename to read from stdin, which allows us to string together the pbpaste and pbcopy commands on OSX to convert JSON or YML in your clipboard to the other format.

# clipboard yml -> json
pbpaste | yq eval -j - | pbcopy

# clipboard json -> yml
pbpaste | yq eval -P - | pbcopy

Did you find this helpful or fun? paypal.me/mrcoles
comments powered by Disqus

Peter Coles

Peter Coles

is a software engineer living in NYC who is building Superset 💪 and also created GoFullPage 📸
more »

github · soundcloud · @lethys · rss