Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem creating nested JSON with python from csv with columns without value

Thanks in advance for helping. I have a csv file with the following structure:

group1,group2,group3,name,info
General,Nation,,Phil,info1
General,Nation,,Karen,info2
General,Municipality,,Bill,info3
General,Municipality,,Paul,info4
Specific,Province,,Patrick,info5
Specific,Province,,Maikel,info6
Specific,Province,Governance,Mike,info7
Specific,Province,Governance,Luke,info8
Specific,District,,Maria,info9
Specific,District,,David,info10

I need a nested JSON for use in D3 or amcharts. With the python script on this page (https://github.com/hettmett/csv_to_json) I could create a nested JSON.

The results looks like this:

[
   {
      "name" : "General",
      "children" : [
         {
            "name" : "Nation",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Phil",
                        "children" : [
                           {
                              "name" : "info1"
                           }
                        ]
                     },
                     {
                        "name" : "Karen",
                        "children" : [
                           {
                              "name" : "info2"
                           }
                        ]
                     }
                  ]
               }
            ]
         },
         {
            "name" : "Municipality",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Bill",
                        "children" : [
                           {
                              "name" : "info3"
                           }
                        ]
                     },
                     {
                        "name" : "Paul",
                        "children" : [
                           {
                              "name" : "info4"
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   },
   {
      "name" : "Specific",
      "children" : [
         {
            "name" : "Province",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Patrick",
                        "children" : [
                           {
                              "name" : "info5"
                           }
                        ]
                     },
                     {
                        "name" : "Maikel",
                        "children" : [
                           {
                              "name" : "info6"
                           }
                        ]
                     }
                  ]
               },
               {
                  "name" : "Governance",
                  "children" : [
                     {
                        "name" : "Mike",
                        "children" : [
                           {
                              "name" : "info7"
                           }
                        ]
                     },
                     {
                        "name" : "Luke",
                        "children" : [
                           {
                              "name" : "info8"
                           }
                        ]
                     }
                  ]
               }
            ]
         },
         {
            "name" : "District",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Maria",
                        "children" : [
                           {
                              "name" : "info9"
                           }
                        ]
                     },
                     {
                        "name" : "David",
                        "children" : [
                           {
                              "name" : "info10"
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
]

However it is not exactly what I need. The problem is that some of the columns do not have a value and therefore no child should be included in de nested JSON. Like this:

"name": "Overview",
  "children": [{
      "name": "General", 
          "children": [
              { "name": "Nation",
                  "children": [
                      {"name": "Phil", "info": "info1"},
                      {"name": "Karen", "info": "info2"} 
                  ]
              },
              { "name": "Municipality",
                  "children": [
                      {"name": "Bill", "info": "info3"},
                      {"name": "Paul", "info": "info4"} 
                  ]        
              }
          ]


}, 
{
      "name": "Specific", 
          "children": [
              { "name": "Province",
                  "children": [
                      {"name": "Patrick", "info": "info5"},
                      {"name": "Maikel", "info": "info6"},
                      {"name": "Governance",
                          "children": [
                              {"name": "Mike", "info": "info7"},
                              {"name": "Luke", "info": "info8"}
                          ]
                      }
                  ]
              },
              { "name": "District",
                  "children": [
                      {"name": "Maria", "info": "info9"},
                      {"name": "David", "info": "info10"} 
                  ]        
              }
          ]
}
]

Hope someone can help.

Kind regards Stefan

like image 650
Stefan Avatar asked Apr 21 '20 05:04

Stefan


1 Answers

There are actually two meaningful differences between your "ideal" result and the result given from the script:

  1. The removal of empty strings (as you noted).
  2. The last child is formatted as: {"name": "xxxxx", "info": "yyyyy"} rather than {"name": "xxxxx", "children": [{"name": "yyyyy"}]}.

So we can solve both of those problems:

Assuming you've defined js_objs as the result of the csv-to-json library you mentioned above.

from copy import deepcopy


def remove_empties(children):
    """Just removes the empty name string levels."""
    for i, js in enumerate(children):
        if js['name'] == '':
            children.pop(i)
            if 'children' in js:
                for child_js in js['children'][::-1]:
                    children.insert(i, child_js)
            if i < len(children):
                js = children[i]
            else:
                raise StopIteration('popped off a cap')
    for i, js in enumerate(children):
        if 'children' in js:
            js['children'] = remove_empties(js['children'])
    return children


def parse_last_child(js):
    """Looks for the last child and formats that one correctly"""
    if 'children' not in js:
        print(js)
        raise ValueError('malformed js')
    if len(js['children']) == 1 and 'children' not in js['children'][0]:
        js['info'] = js.pop('children')[0]['name']
    else:
        js['children'] = [parse_last_child(j) for j in js['children']]
    return js


accumulator = deepcopy(js_objs)  # so we can compare against the original
non_empties = remove_empties(accumulator)
results = [parse_last_child(x) for x in non_empties]

And the results I get are...

[{'name': 'General',
  'children': [{'name': 'Nation',
    'children': [{'name': 'Phil', 'info': 'info1'},
     {'name': 'Karen', 'info': 'info2'}]},
   {'name': 'Municipality',
    'children': [{'name': 'Bill', 'info': 'info3'},
     {'name': 'Paul', 'info': 'info4'}]}]},
 {'name': 'Specific',
  'children': [{'name': 'Province',
    'children': [{'name': 'Patrick', 'info': 'info5'},
     {'name': 'Maikel', 'info': 'info6'},
     {'name': 'Governance',
      'children': [{'name': 'Mike', 'info': 'info7'},
       {'name': 'Luke', 'info': 'info8'}]}]},
   {'name': 'District',
    'children': [{'name': 'Maria', 'info': 'info9'},
     {'name': 'David', 'info': 'info10'}]}]}]

Note: This will work as long as your json objects aren't too deep. Otherwise you'll hit the recursion depth.

Just to clarify, in this case:

js_objs = [
   {
      "name" : "General",
      "children" : [
         {
            "name" : "Nation",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Phil",
                        "children" : [
                           {
                              "name" : "info1"
                           }
                        ]
                     },
                     {
                        "name" : "Karen",
                        "children" : [
                           {
                              "name" : "info2"
                           }
                        ]
                     }
                  ]
               }
            ]
         },
         {
            "name" : "Municipality",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Bill",
                        "children" : [
                           {
                              "name" : "info3"
                           }
                        ]
                     },
                     {
                        "name" : "Paul",
                        "children" : [
                           {
                              "name" : "info4"
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   },
   {
      "name" : "Specific",
      "children" : [
         {
            "name" : "Province",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Patrick",
                        "children" : [
                           {
                              "name" : "info5"
                           }
                        ]
                     },
                     {
                        "name" : "Maikel",
                        "children" : [
                           {
                              "name" : "info6"
                           }
                        ]
                     }
                  ]
               },
               {
                  "name" : "Governance",
                  "children" : [
                     {
                        "name" : "Mike",
                        "children" : [
                           {
                              "name" : "info7"
                           }
                        ]
                     },
                     {
                        "name" : "Luke",
                        "children" : [
                           {
                              "name" : "info8"
                           }
                        ]
                     }
                  ]
               }
            ]
         },
         {
            "name" : "District",
            "children" : [
               {
                  "name" : "",
                  "children" : [
                     {
                        "name" : "Maria",
                        "children" : [
                           {
                              "name" : "info9"
                           }
                        ]
                     },
                     {
                        "name" : "David",
                        "children" : [
                           {
                              "name" : "info10"
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
]
like image 99
Aaron Avatar answered Oct 13 '22 04:10

Aaron