0

I have CSV file (data.csv) which contains a column with its parent id. Based on this file, I would like to create one or many json files which contain the root and all its children. An example for a CSV file with one root only:

BrandID,Name,ParentBrandID
1,Brand,
2,Normal Brand,1
3,Bad Brand,1
4,Best Brand,1
5,Cleaner,4
6,Fat Remover,4
7,Others,1
8,Kitchen,7

And I need to convert that file to a json format that looks like this:

    {
        "name":"Brand",
        "children":[
            {
                "name":"Normal Brand"
            },
            {
                "name":"Bad Brand"
            },
            {
                "name":"Best Brand",
                "children":[
                    {
                        "name":"Cleaner"
                    },
                    {
                        "name":"Fat Remover"
                    }
                ]
            },
            {
                "name":"Others",
                "children":[
                    {
                        "name":"Kitchen"
                    }
                ]
            }
        ]
    }

Is there an straight forward way to do this?

If it helps for anything, I need this to use as an entry for d3 libraries.

Thanks!

  • Can you please provide the code you have written and an explanation of how it's failing then someone may be able to assist. Have a look at the Asking session in the Help pages: https://stackoverflow.com/help – Lazarus Jan 31 '18 at 09:31
  • Possible duplicate of [Python CSV to JSON](https://stackoverflow.com/questions/19697846/python-csv-to-json) – Giorgos Myrianthous Jan 31 '18 at 09:32
  • I haven't written any particular code to solve this. The question is if there's any method that does this already? Otherwise I can develop my own converter. – Mario Antonio Alvarez Enriquez Jan 31 '18 at 20:19

0 Answers0