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? Please donate!

donate via btc or eth

btc: 18jCzwsZDGQYcs6Kyv92pd4683cnnxm1Dd
eth: 0xC285F21Cb271Cb4B3F70c4C47B2f7B26063AF590
paypal: 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