Faber
Faber

Reputation: 380

Mapping csv file to nested json

I have data in a csv in the form of

"category1", 2010, "deatil1"
"category1", 2010, "deatil2"
"category1", 2011, "deatil3"
"category2", 2011, "deatil4"

I need to map it to json in form of

 {
        "name": "Data",
        "children": [{
            "name": "category1",
            "children": [{
                "name": "2010",
                "children": [
                    {"name": "deatil1"}, 
                    {"name": "detail2"}
                 ],
                "name": "2011",
                "children": [
                    {"name": "detail3"}
                ]
            }, {
            }]
        }, 
            {
            "name": "category2",
            "children": [{
                    "name": "2011",
                    "children": [{
                        "name": "detail4"
                    }]
                }
            ]
        }
    ]
    }

Basically I need to collect up all the details for each unique category and year pair and put the list

I have tried to use a nested dict structure but the output is not correct.

I have created a custom dict class that handles the nesting of the nesting of the dictionaries. The following code collects the data in the right structure, but I am unsure how to proceed with outputting it in the right format. Any help would be greatly appreciated.

class Vividict(dict):

    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

dict = Vividict()

for row in ws.iter_rows(row_offset=1):
    sector = row[0].value
    year =  row[2].value
    detail = row[1].value
    dict[sector][year][detail]

print json.dumps(dict).encode('utf8')

Upvotes: 1

Views: 1185

Answers (1)

sal
sal

Reputation: 3593

Starting from your dict structure, it's a matter of building up the new data structure. I am using here mostly list comprehensions where dict are created:

import json

rows = [
    ("category1", 2010, "deatil1"),
    ("category1", 2010, "deatil2"),
    ("category1", 2011, "deatil3"),
    ("category2", 2011, "deatil4")]

class Vividict(dict):
    def __missing__(self, key):
        value = self[key] = type(self)()
        return value

dict = Vividict()

for row in rows:
    sector = row[0]
    year = row[1]
    detail = row[2]
    dict[sector][year][detail]

# This is the new data structure, derived from the existing 'dict'
d = {'name': 'Data',
     'children': [
         {'name': k1,  # sector
          'children': [
                       {'name': k2,  # year
                        'children': [
                            {
                             'name': k3  # deatil
                             } for k3 in v2.keys()]
                        } for k2, v2 in v1.iteritems()]
          } for k1, v1 in dict.iteritems()]
     }

print json.dumps(d).encode('utf8')

See it in action here: https://eval.in/662805

Upvotes: 4

Related Questions