Mac cli to convert yaml to json

Viewed 7988

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

5 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 https://github.com/mikefarah/yq

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

Install yq , yaml2json wont work for mac

brew install yq

Then use it:

yq -o=json test-env.yml > test-env.json

Here test-env.json is the output file

Related