Skip to content

Instantly share code, notes, and snippets.

@mboersma
Last active July 31, 2023 12:34
Show Gist options
  • Save mboersma/1329669 to your computer and use it in GitHub Desktop.
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))'
@Dimwest
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
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
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
Copy link

germn commented Dec 7, 2018

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

@ptdel
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
Copy link

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

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

@oxr463
Copy link

oxr463 commented Dec 16, 2019

it appears that it comes in random bursts

I mean, that is pretty standard for many projects.

@Circuitsoft
Copy link

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