import json
xyz={"john": """{"name": "john","id":"123"}""","tom" : """{"name":"tom","id":"456"}"""}
class abc(object):
def __init__ (self,**d):
self.name=d['name'];
self.id=d['id'];
def main():
ks=xyz.keys()
for j in ks:
lm1="xyz['%s']" %(j)
ds=eval(lm1);
ds1=json.loads(ds)
ln="%s=abc(**ds1)" %(j)
print(ln)
exec(ln);
ln2="%s.name" %(j)
print(eval(ln2));
print(john.name)
print(tom.id)
if __name__ == "__main__":
main();
and the error is
tom=abc(**ds1)
tom
john=abc(**ds1)
john
Traceback (most recent call last):
File "new6.py", line 26, in <module>
main();
File "new6.py", line 22, in main
print(john.name)
NameError: name 'john' is not defined
why am i not being able to access "tom.name","john.name" in main() block? where did i do wrong? and how can it be done in much simpler way? (i actually have a json file, dont bother much about the "xyz")