Last active
September 2, 2020 14:14
-
-
Save coreequip/779496f330c6f72902b74ccdcdec3a32 to your computer and use it in GitHub Desktop.
Convert a multi-level JSON to a CSV and back
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 python3 | |
import csv | |
import json | |
import os | |
import sys | |
if len(sys.argv) < 2: | |
print('Usage: {} <convertfile.(json|csv)>'.format(os.path.basename(sys.argv[0]))) | |
exit(0) | |
if not os.path.exists(sys.argv[1]): | |
print('File {} doesnt exists!'.format(sys.argv[1])) | |
exit(1) | |
srcFile = os.path.abspath(sys.argv[1]) | |
def flattenjson(j, delim): | |
result = [] | |
for i in j.keys(): | |
if isinstance(j[i], dict): | |
for k in flattenjson(j[i], delim): | |
result.append(i + delim + k) | |
else: | |
result.append(i + delim + j[i]) | |
return result | |
def deflattencsv(j, row, i): | |
if len(row) == i + 1: | |
return | |
if not row[i] in j: | |
j[row[i]] = {} | |
if len(row) > i + 2 and row[i + 2] is not '': | |
j[row[i]] = deflattencsv(j[row[i]], row, i + 1) | |
else: | |
j[row[i]] = row[i + 1] | |
return j | |
if srcFile.lower().endswith('.json'): | |
destFile = os.path.splitext(srcFile)[0] + '.csv' | |
with open(srcFile, 'r', encoding='utf-8-sig') as input_file: | |
j = json.load(input_file) | |
with open(destFile, 'w', encoding='utf-8-sig') as output_file: | |
output_file.write(('\n'.join(sorted(flattenjson(j, ';'))))) | |
else: | |
result = {} | |
destFile = os.path.splitext(srcFile)[0] + '.json' | |
with open(srcFile, 'r', encoding='utf-8-sig') as input_file: | |
csv_reader = csv.reader(input_file, delimiter=';') | |
for row in csv_reader: | |
deflattencsv(result, row, 0) | |
with open(destFile, 'w', encoding='utf8') as output_file: | |
json.dump(result, output_file, sort_keys=True, indent=4, ensure_ascii=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment