Skip to content

Instantly share code, notes, and snippets.

@zerok
Created May 14, 2017 23:01
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 zerok/0a39f63cb3ad337bf3b2e40ce2a43975 to your computer and use it in GitHub Desktop.
Save zerok/0a39f63cb3ad337bf3b2e40ce2a43975 to your computer and use it in GitHub Desktop.
Converter for WTD data from JSON to YAML
"""
This script is used to convert the JSON data we have on each conference into
YAML.
"""
import collections
import glob
import io
import json
import yaml
import os.path
# Title should be listed before the abstract and other things to make
# collapsing content in editor that support it much more usable.
order = ['title', 'name', 'abstract', 'speakers', 'youtubeId']
# Source: http://stackoverflow.com/a/16782282
def represent_ordereddict(dumper, data):
result = []
for item_key, item_value in data.items():
node_key = dumper.represent_data(item_key)
node_value = dumper.represent_data(item_value)
result.append((node_key, node_value))
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', result)
yaml.add_representer(collections.OrderedDict, represent_ordereddict)
def create_sorted_item(item):
result = []
found = set()
for key in order:
if key in item:
result.append((key, item[key]))
found.add(key)
for key, value in item.items():
if key not in found:
result.append((key, item[key]))
return collections.OrderedDict(result)
for json_file in glob.glob('_data/*.json'):
basename = os.path.basename(json_file)
folder = os.path.dirname(json_file)
target_file = os.path.join(folder, "{}.yaml".format(os.path.splitext(basename)[0]))
print("{} -> {}".format(json_file, target_file))
with io.open(json_file) as fp:
data = json.load(fp)
converted_data = []
if isinstance(data, list):
for item in data:
converted_data.append(create_sorted_item(item))
else:
converted_data = data
with io.open(target_file, "w+") as out:
output = yaml.dump(converted_data, default_flow_style=False)
final_output = []
for line in output.split('\n'):
# Remove EOL characters like \
final_output.append(line.rstrip('\\').replace('\\ ', ''))
out.write('\n'.join(final_output))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment