Skip to content

Instantly share code, notes, and snippets.

@lmlsna
Created February 7, 2019 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmlsna/a42dfc350594edae085d93bde9453763 to your computer and use it in GitHub Desktop.
Save lmlsna/a42dfc350594edae085d93bde9453763 to your computer and use it in GitHub Desktop.
Convert JSON to YAML
#!/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