Skip to content

Instantly share code, notes, and snippets.

@mboersma mboersma/yaml2json
Last active Oct 18, 2019

Embed
What would you like to do?
YAML to JSON one-liner
python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print json.dumps(y)'
@jwilk

This comment has been minimized.

Copy link

jwilk commented May 17, 2018

Beware that yaml.load() can execute arbitrary code embedded in the YAML input.
Consider using yaml.safe_load() instead.

@jaytaylor

This comment has been minimized.

Copy link

jaytaylor commented Jul 25, 2018

@jwilk good idea.

python -c 'import json, sys, yaml ; y=yaml.safe_load(sys.stdin.read()) ; print(json.dumps(y))'
@aureooms

This comment has been minimized.

Copy link

aureooms commented Aug 28, 2018

Without print?

python -c 'import json, sys, yaml ; y=yaml.safe_load(sys.stdin.read()) ; json.dump(y, sys.stdout)'
@vongohren

This comment has been minimized.

Copy link

vongohren commented Aug 31, 2018

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
@Dimwest

This comment has been minimized.

Copy link

Dimwest commented Sep 1, 2018

You most likely need to install the yaml module, try to run "pip install yaml" in your terminal, that should do the job

@jwilk

This comment has been minimized.

Copy link

jwilk commented Sep 5, 2018

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.

@gorlovka

This comment has been minimized.

Copy link

gorlovka commented Nov 2, 2018

doesn't work for me, no idea why
no errors or warnings are given

http://kagda.ru/i/f101718f1290_02-11-2018-18:51:09_f101.png

@germn

This comment has been minimized.

Copy link

germn commented Dec 7, 2018

Don't use pyyaml, use ruamel.yaml instead. Reason.

@ptdel

This comment has been minimized.

Copy link

ptdel commented Jan 4, 2019

@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.

@wonderbeyond

This comment has been minimized.

Copy link

wonderbeyond commented Mar 5, 2019

$ alias yaml2json="python -c 'import sys, yaml, json; y=yaml.load(sys.stdin.read()); print(json.dumps(y))'"
$ cat ~/.kube/config | yaml2json | jq .
@turing4ever

This comment has been minimized.

Copy link

turing4ever commented Mar 19, 2019

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

@fdabek1

This comment has been minimized.

Copy link

fdabek1 commented May 15, 2019

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.