Created
December 16, 2016 20:29
-
-
Save yarko/caac2db64da2ff91a0fb430ec09aad8e to your computer and use it in GitHub Desktop.
Simple json / yaml conversions
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 python | |
from os import path | |
import sys | |
import json | |
import yaml | |
if len(sys.argv) > 1: | |
print("usage:\n\t{} < your_json_file > your_yaml_file".format( | |
path.basename(sys.argv[0]))) | |
sys.exit(1) | |
yaml.dump(json.load(sys.stdin), sys.stdout, default_flow_style=False) | |
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 python | |
import sys | |
import json | |
import yaml | |
import optparse | |
## | |
# yaml2json - simple tool to convert yaml to json | |
# | |
# only operates on stdin / stdout; | |
# - takes an option to make a readable output (i.e. take an indent arg) | |
# | |
INDENT = 2 # default | |
def yml2json(indent=None): | |
wc = yaml.safe_load(sys.stdin) | |
json.dump(wc, sys.stdout, indent=indent) | |
if __name__ == '__main__': | |
usage = """ useage: %prog [-i] | |
this is a pipeline tool, using STDIN and STDOUT.""" | |
parser = optparse.OptionParser(usage=usage) | |
parser.add_option('-i', '--indent', | |
action="store", type="int", | |
help="generate human readable [indent]ed output") | |
opts, args = parser.parse_args() | |
yml2json(opts.indent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment