-
-
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))' |
@jwilk good idea.
python -c 'import json, sys, yaml ; y=yaml.safe_load(sys.stdin.read()) ; print(json.dumps(y))'
Without print
?
python -c 'import json, sys, yaml ; y=yaml.safe_load(sys.stdin.read()) ; json.dump(y, sys.stdout)'
Im getting this error, any reason to that? Very noob in python btw 😄
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named yaml
You most likely need to install the yaml module, try to run "pip install yaml" in your terminal, that should do the job
pip install yaml
There is no project called yaml
on PyPI. And since the name is not taken, anybody could upload malware under that name…
The project name for the yaml
module is PyYAML.
If you are a newcomer, you probably should stay very very far away from pip, and use your distro's package manager to install Python modules.
doesn't work for me, no idea why
no errors or warnings are given
Don't use pyyaml
, use ruamel.yaml
instead. Reason.
@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)'
Beware that
yaml.load()
can execute arbitrary code embedded in the YAML input.Consider using
yaml.safe_load()
instead.