Reorganized JSON
- by couatl
Need to be reorganized JSON into a new structure. Python.
for example
{
'a' : 1,
'b' : 1,
'd' : {'d1' : '1', 'd2' : 2},
'm' : [
{'x' : 6, 'y' : 5, 'z' : {'foo' : 'foo1', 'bar : 'bar1'} },
{'x' : 8, 'y' : 8, 'z' : {'foo' : 'foo2', 'bar : 'bar2'} } ...
]
}
to
{
'new_a' : 1,
'new_d' : {'new_d1' : '1', 'new_d2' : 2},
'new_m' : [
{'new_x' : 6, 'new_z' : {'new_foo' : 'foo1', new_'bar : 'bar1'} },
{'new_x' : 8, 'new_z' : {'new_foo' : 'foo2', 'new_bar : 'bar2'} } ...
]
}
There is the idea of ??a new form of an old JSON
Is there a more elegant way of that?
import json
new_data = {}
new_data['new_a'] = old_data['a']
new_data['new_d'] = {}
new_data['new_d']['new_d1'] = old_data['d']['d1']
new_data['new_d']['new_d2'] = old_data['d']['d2']
new_data['new_m'] = {}
new_m = []
for m in old_data:
new_m.append({'new_x' : m['x'], 'new_z' : {'new_foo' ....