Commit 2d7a73c7 authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Added scripts to merge and split json documents

parent eb487842
#!/usr/bin/env python3
import json
import argparse
parser = argparse.ArgumentParser(description='Join json documents')
parser.add_argument('jsons', metavar='N', nargs='+', help='json documents')
parser.add_argument('--output', '-o', required=True, help='The name of the output document')
args = parser.parse_args()
joined = {}
for file in args.jsons:
with open(file) as f:
doc = json.load(f)
for k in doc:
if not k in joined:
joined[k] = {'id': k, 'computations':[]}
joined[k]['computations'].extend(doc[k]['computations'])
output_filename = args.output
with open(output_filename, 'w') as o:
json.dump(joined, o)
#!/usr/bin/env python3
import json
import argparse
import os
import os.path
parser = argparse.ArgumentParser(description='Split a json file with multiple documents into single files per document')
parser.add_argument('--json', '-j', required=True, help='json documents')
parser.add_argument('--output', '-o', required=True, help='The name of the output directory')
args = parser.parse_args()
# check if output directory exists and create it if not
output_dir = args.output
if os.path.exists(output_dir) and os.path.isdir(output_dir):
print("Output exists and is not a directory")
exit(1)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
file = args.json
with open(file) as f:
doc = json.load(f)
for k in doc:
output_filename = k + '.json'
with open(output_dir + '/' + output_filename, 'w') as o:
json.dump(doc[k], o)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment