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