Mapping csv file to nested json
<p>I have data in a csv in the form of </p>

<pre><code>"category1", 2010, "deatil1"
"category1", 2010, "deatil2"
"category1", 2011, "deatil3"
"category2", 2011, "deatil4"
</code></pre>

<p>I need to map it to json in form of </p>

<pre><code> {
        "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"
                    }]
                }
            ]
        }
    ]
    }
</code></pre>

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

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

<p>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.</p>

<pre><code>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')
</code></pre>

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

<pre><code>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')
</code></pre>

<p>See it in action here: <a href="https://eval.in/662805" rel="nofollow">https://eval.in/662805</a></p>

python json csv dictionary