-
-
Save mboersma/1329669 to your computer and use it in GitHub Desktop.
python3 -c 'import sys, yaml, json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))' |
@germn , that SO post: answered Apr 21 '16 at 5:32
and if you look at the PyYAML page:
2018-06-24: LibYAML 0.2.1 is released.
2016-08-28: LibYAML 0.1.7 and PyYAML 3.12 are released.
also...
tl;dr
Always use ruamel.yaml. Never use PyYAML. ruamel.yaml lives. PyYAML is a fetid corpse rotting in the mouldering charnel ground of PyPi.
Long live ruamel.yaml.
leads me to think this is more a matter of opinion than anything else. If you prefer ruamel thats fine, just be aware that the reason you provide isn't really a valid one.
$ alias yaml2json="python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print(json.dumps(y))'"
$ cat ~/.kube/config | yaml2json | jq .
It will be faster if you use CLoader:
python3 -c 'import json, sys, yaml;from yaml import CSafeLoader as sl; y=yaml.load(sys.stdin.read(), Loader=sl) ; json.dump(y, sys.stdout)' && echo
@ptdel, I would look at the comments of the answer on StackOverflow. PyYAML development abruptly halted again and their release at the time of the comment broke many projects. While development seems to have resumed again, it appears that it comes in random bursts which is not always the best thing as a developer that is looking to have a pull request or other bugs resolved.
it appears that it comes in random bursts
I mean, that is pretty standard for many projects.
python3 -c 'import sys, yaml, json; json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=4)'
I usually want the pretty-print functionality.
If you only want pretty-print, and don't need JSON...
python3 -c 'import sys, yaml; yaml.dump(yaml.safe_load(sys.stdin), sys.stdout)'
Don't use
pyyaml
, useruamel.yaml
instead. Reason.