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