I have a .json file with a complex structure: dict in a dict, it has no constant structure and is dynamically changed.
The goal is to make new dict where the keys are numbers of hierarchy and values must be strings that are composed of all previous steps like
2 drinks
2.1 drinks coffee
2.1.1 drinks coffee instant
2.1.2 drinks coffee real
2.2 drinks tea
2.3 drinks water
I wrote a script, and it works. But as I am new to Python and in programming, please check it out and correct me in order to code may look and perform more elegantly and in a pythonic way.
Also I would like to decrease time of execution because the input .json file is going to be quite big.
Sample cat.json file:
{
"communication":
{"mobile":{
"vodafone":{"subscr":"","txt":"","mms":"","internet":"","calls":
{"in":{"home":"","roaming":""},"out":{"home":"","roaming":""}}},
"verizon":{"subscr":"","txt":"","mms":"","internet":"1Gb","calls":
{"in":{"home":"","roaming":""},"out":{"home":"500 min","roaming":"Other country"}}}},
"internet":"SomeProviderName"
},
"food":{"dairy":{"cheese":"Gauda","milk":{"brand":"name","origin":"place"}}},
"drinks":
{
"water":"",
"tea":"",
"coffee":
{
"instant":"",
"real":""
}}}
Script:
#!/usr/bin/env python -tt
# -*- coding: utf-8 -*-
import json
filetoread='cat.json'
def load_existed(filetoread):
try:
data=json.loads(open(filetoread).read())
return data
except ValueError:
print 'data loading error'
cat_data=load_existed(filetoread)
def walk_dict(d,mess,ln,new_dict,crumbs):
inter=1
lc=list(mess)
last_crumb=crumbs.split( )
for k,v in sorted(d.items(),key=lambda x: x[0]):
if mess=='':
mess=str(inter)
lc=list(mess)
last_crumb=crumbs.split( )
if isinstance(v, dict) :
ln=len(v)
lc[len(lc)-1]=str(inter)
mess="".join(lc)
crumbs=" ".join(last_crumb)
if len(crumbs.split( ))>0:
crumbs=crumbs+" "+k
else:
crumbs=k
#print mess,'-->',k,">"
new_dict[mess]=crumbs
mess=mess+'.1'
walk_dict(v,mess,ln,new_dict,crumbs)
else:
if ln>0:
ln=ln-1
lc[len(lc)-1]=str(inter)
mess="".join(lc)
crumbs=" ".join(last_crumb)
crumbs=crumbs+" "+k+" "+v
#print mess,'-->',"%s -> %s" % (k, v)
new_dict[mess]=crumbs
inter=inter+1
return new_dict
new_data=walk_dict(cat_data,"",0,{},'')
print "-"*30
#print new_data
for v in sorted(new_data):
print v, new_data[v]