Skip to content

Instantly share code, notes, and snippets.

@itzikBraun
Created July 24, 2017 15:12
Show Gist options
  • Save itzikBraun/195ef7d42bf406c60f9627f553c261d3 to your computer and use it in GitHub Desktop.
Save itzikBraun/195ef7d42bf406c60f9627f553c261d3 to your computer and use it in GitHub Desktop.
jsonToYaml
"""
Convery JSON files to YAML
depends on pyyaml, pip install pyyaml
"""
import sys
import json
from os import listdir, rename
from os.path import isdir, join, isfile, splitext
import yaml
def _convert_file(file_path):
if file_path.endswith(".json"):
with open(file_path, 'r+') as f:
json_input = f.read()
print splitext(file_path)[0]
yaml_output = yaml.dump(yaml.load(json.dumps(json.loads(json_input))), default_flow_style=False)
f.seek(0)
f.write(yaml_output)
f.truncate()
rename(file_path, splitext(file_path)[0] + ".yml")
print yaml_output
def _convert_dir(dir_path):
for file_path in listdir(dir_path):
file_path = join(dir_path, file_path)
convert(file_path)
def convert(path):
if isdir(path):
_convert_dir(path)
elif isfile(path):
_convert_file(path)
convert(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment