Mac Cli to Convert Yaml to Json

Mac Cli to Convert Yaml to Json

is there a quick way to convert bunch of yaml files to json files. I looked at yaml2json and it is not working (throws some exception)

Thanks

1

4 Answers

Adding answer because there's no answer for the latest version for yq (except in comments).

brew install yq

yq -j eval test.yaml
1

Use the real yq (not python yq) from

brew reinstall yq

Then run:

/usr/local/bin/yq eval deployment.yaml -o=json -P > deployment.json

Meaning of the arguments, from --help:

  -P, --prettyPrint           pretty print, shorthand for '... style = ""'
  -o, --output-format string  [yaml|y|json|j|props|p|xml|x] output format type. (default "yaml") 
4

You could pipe your YAML to this (requires having pyYAML installed)

python -c 'import yaml; import json; import sys; print(json.dumps(yaml.safe_load(sys.stdin)));'

With current releases of yq (v4.24.5):

yq eval -o json my.yaml > my.json

Note that (as of 4.18.1) eval is the default command when none is supplied to yq, which means that this works perfectly fine, as well.

yq -o json my.yaml > my.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.