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...
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
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
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")
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