python 3.x - Flatten a unserialized nested json data having multilevel dictionary nesting at large -
i have data structure 1 mentioned below nested in terms of dictionaries : { 'list':{ '5':{ 'set_profile_type':'static', 'status_name_now':'off', 'name':'btテザリング', 'status_code_now':'off', 'status_name_init':'off', 'status_list':{ '0':{}, '1':{} }, 'guide':'some utf text', 'choice_type':'select', 'get_profile':'bttethering_getonoff', 'status_key_name':'onoff' }, '4':{ 'set_profile_type':'static', 'status_name_now':'off', 'name':'wifiテザリング', 'status_code_now':'off', 'status_name_init':'off', 'status_list':{ '0':{}, '1':{} }, 'guide':'utf text', 'choice_type':'select', 'get_profile':'portablehotspot_getonoff', 'status_key_name':'onoff' }, '2':{}, '0':{}, '6':{}, '1':{}, '3':{} }, 'name':'somepropert1' }
while trying flatten structure csv, have tried pandas library json.loads, above string formed result of ohpserialization library have used unserialize; there nice , clean way flatten data structure have tried json_normalize doesn't recognize above string json-
def deserialize(data): import phpserialize import ast import json data = "'"+data+"'" data = ast.literal_eval(format(data)).encode('utf-8') j1 = phpserialize.unserialize(data) j1 = phpserialize.loads(phpserialize.dumps(j1), decode_strings=true) j1 = json.dumps(j1) return j1 def customparser(data): import json j1 = json.loads(data) j1 = j1 return j1 df = pandas.read_csv(input_file,sep="|",converters= {'device_udate':deserialize},header=0) print (df) df.to_csv(output_file,sep=",",encoding="utf-8") df1 = pandas.read_csv(output_file,converters= {'device_udate':customparser},header=0) df1[sorted(df1['device_udate'][0].keys())] = df1['device_udate'].apply(pandas.series) print (df1)
wiki
Comments
Post a Comment