Reorganized JSON

Posted by couatl on Stack Overflow See other posts from Stack Overflow or by couatl
Published on 2013-11-08T03:50:31Z Indexed on 2013/11/08 3:53 UTC
Read the original article Hit count: 79

Filed under:
|

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' ....

© Stack Overflow or respective owner

Related posts about python

Related posts about JSON