Skip to content

Instantly share code, notes, and snippets.

@mboersma
Last active July 31, 2023 12:34
Show Gist options
  • Select an option

  • Save mboersma/1329669 to your computer and use it in GitHub Desktop.

Select an option

Save mboersma/1329669 to your computer and use it in GitHub Desktop.
YAML to JSON one-liner
python3 -c 'import sys, yaml, json; y=yaml.safe_load(sys.stdin.read()); print(json.dumps(y))'
@jwilk

jwilk commented May 17, 2018

Copy link
Copy Markdown

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

@jaytaylor

Copy link
Copy Markdown

@jwilk good idea.

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

@make-github-pseudonymous-again

Copy link
Copy Markdown

Without print?

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

@vongohren

Copy link
Copy Markdown

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

Dimwest commented Sep 1, 2018

Copy link
Copy Markdown

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

@jwilk

jwilk commented Sep 5, 2018

Copy link
Copy Markdown

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

gorlovka commented Nov 2, 2018

Copy link
Copy Markdown

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

germn commented Dec 7, 2018

Copy link
Copy Markdown

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

@pzybe

pzybe commented Jan 4, 2019

Copy link
Copy Markdown

@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

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

@turing4ever

turing4ever commented Mar 19, 2019

Copy link
Copy Markdown

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

fdabek1 commented May 15, 2019

Copy link
Copy Markdown

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

@oxr463

oxr463 commented Dec 16, 2019

Copy link
Copy Markdown

it appears that it comes in random bursts

I mean, that is pretty standard for many projects.

@Circuitsoft

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment