Skip to content Skip to sidebar Skip to footer

Convert The Nested Json Into A Dictionary Format With No Nested Objects

I have the input data in below format: data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {

Solution 1:

you can try:

1) in python3:

from pprint import pprint

data = [["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
key_list = ["Name", "Age", "Children", "details"]

pprint([dict(zip(key_list[:2], e[:2]), **{key_list[2]: ','.join(e[2])}, **e[3]) for e in data])

output:

[{'Name': 'Richard',
  'Age': '48',
  'Children': 'Josh,Beth',
  'city': 'Seattle',
  'Disability': 'no',
  'enterprenuer': 'yes'},
 {'Name': 'Bryan',
  'Age': '32',
  'Children': '',
  'city': 'NY',
  'enterprenuer': 'no',
  'wfh': 'yes',
  'disability': 'no',
  'Visa': 'no'}]

2) in python2:

pprint([dict(zip(key_list[:2], e[:2]), **dict([(key_list[2], ','.join(e[2]))], **e[3])) for e in data])

output:

[{'Age': '48',
  'Children': 'Josh,Beth',
  'Disability': 'no',
  'Name': 'Richard',
  'city': 'Seattle',
  'enterprenuer': 'yes'},
 {'Age': '32',
  'Children': '',
  'Name': 'Bryan',
  'Visa': 'no',
  'city': 'NY',
  'disability': 'no',
  'enterprenuer': 'no',
  'wfh': 'yes'}]

Solution 2:

data = [["Richard", "48", ["Josh", "Beth"], {"city": "Seattle", "enterprenuer": "yes", "Disability": "no"}], ["Bryan", "32", [], {"Visa": "no", "city": "NY", "wfh": "yes", "enterprenuer": "no", "disability": "no"}]]
key_list = ["Name", "Age", "Children", "details"]

out = []
for item in data:
    d = {}
    out.append(d)
    for value, keyname inzip(item, key_list):
        ifisinstance(value, dict):
            d.update(**value)
        elifisinstance(value, list):
            d[keyname] = ','.join(value)
        else:
            d[keyname] = value

from pprint import pprint
pprint(out)

Prints:

[{'Age': '48',
  'Children': 'Josh,Beth',
  'Disability': 'no',
  'Name': 'Richard',
  'city': 'Seattle',
  'enterprenuer': 'yes'},
 {'Age': '32',
  'Children': '',
  'Name': 'Bryan',
  'Visa': 'no',
  'city': 'NY',
  'disability': 'no',
  'enterprenuer': 'no',
  'wfh': 'yes'}]

Solution 3:

You can use simple unpacking:

data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
key_list = ["Name", "Age", "Children", "details"]
r = [{**dict(zip(key_list[:-1], a[:-1]+[','.join(a[-1])])), **b} for *a, b in data]

Output:

[{'Name': 'Richard', 'Age': '48', 'Children': 'Josh,Beth', 'city': 'Seattle', 'Disability': 'no', 'enterprenuer': 'yes'}, {'Name': 'Bryan', 'Age': '32', 'Children': '', 'city': 'NY', 'enterprenuer': 'no', 'wfh': 'yes', 'disability': 'no', 'Visa': 'no'}]

Edit: Python2.7 solution:

data = [[u'Richard', u'48', [u'Josh', u'Beth'], {u'city': u'Seattle', u'Disability': u'no', u'enterprenuer': u'yes'}], [u'Bryan', u'32',[], {u'city': u'NY', u'enterprenuer': u'no', u'wfh': u'yes', u'disability': u'no', u'Visa': u'no'}]]
key_list = ["Name", "Age", "Children", "details"] 
r = [dict(zip(key_list[:-1], i[:2]+[','.join(i[2])])+i[-1].items()) for i in data]

Output:

[{u'city': u'Seattle', 'Name': u'Richard', 'Age': u'48', u'enterprenuer': u'yes', u'Disability': u'no', 'Children': u'Josh,Beth'}, {u'city': u'NY', u'wfh': u'yes', 'Name': u'Bryan', 'Age': u'32', u'enterprenuer': u'no', u'disability': u'no', u'Visa': u'no', 'Children': ''}]

Post a Comment for "Convert The Nested Json Into A Dictionary Format With No Nested Objects"