Created
February 7, 2019 04:17
-
-
Save lmlsna/a42dfc350594edae085d93bde9453763 to your computer and use it in GitHub Desktop.
Convert JSON to YAML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Accepts a JSON file path as an optional argument, otherwise reads stdin | |
import os | |
import simplejson | |
import sys | |
import yaml | |
# If valid path to json file is passed | |
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]): | |
f = open(sys.argv[1]) | |
data = f.read() | |
f.close() | |
# Otherwise read from stdin | |
else: | |
data = str(sys.stdin.read()) | |
# Print YAML | |
print(yaml.dump(simplejson.loads(data), default_flow_style=False)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment