Last active
October 17, 2019 13:14
-
-
Save saleem-mirza/895fce2e40daecaea8e6f8c5e5fc9002 to your computer and use it in GitHub Desktop.
Convert Json to flat CSV
This file contains 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
import pandas as pd | |
from pandas.io import json | |
from pandas.io.json import json_normalize | |
with open('data.json') as f: | |
data = json.loads(f.read()) | |
def normalize(o): | |
dict_obj = {} | |
items = [] | |
list_items = [] | |
def inner(x, key): | |
for k, v in x.items(): | |
if key == '': | |
item_key = k | |
else: | |
item_key = '{}.{}'.format(key, k) | |
if isinstance(v, dict): | |
inner(v, item_key) | |
else: | |
if isinstance(v, list): | |
list_items.append(item_key) | |
dict_obj[item_key] = v | |
for item in o: | |
if isinstance(item, dict): | |
inner(item, '') | |
if len(list_items) > 0: | |
_list_l1 = [] | |
_list_ln = [] | |
list_items.sort() | |
col_key = list_items.pop() | |
if len(dict_obj[col_key]) > 0: | |
for _v in dict_obj[col_key]: | |
t = dict_obj.copy() | |
t[col_key] = _v | |
_list_l1.append(t) | |
else: | |
t = dict_obj.copy() | |
t[col_key] = None | |
_list_l1.append(t) | |
if len(list_items) > 0: | |
while len(list_items) > 0: | |
col_key = list_items.pop() | |
for _v in dict_obj[col_key]: | |
for _l in _list_l1: | |
t = _l.copy() | |
t[col_key] = _v | |
_list_ln.append(t) | |
_list_l1 = _list_ln.copy() | |
_list_ln.clear() | |
if len(_list_l1) > 0: | |
items.extend(_list_l1) | |
else: | |
items.append(dict_obj) | |
del item | |
return items | |
data = normalize(data) | |
df = json_normalize(data) | |
df.to_csv('data.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment