'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



Solution 1:[1]

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

brew install yq

yq -j eval test.yaml

Solution 2:[2]

Use the real yq (not python yq) from https://github.com/mikefarah/yq

brew reinstall yq

Then run:

/usr/local/bin/yq r deployment.yaml -j -P > deployment.json

Meaning of the arguments, from --help:

  -P, --prettyPrint    pretty print, shorthand for '... style = ""'
  -j, --tojson         output as json. Set indent to 0 to print json in one line 

Solution 3:[3]

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)));'

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amay
Solution 2 cubuspl42
Solution 3 Andrew
Solution 4