Python flatten multilevel/nested JSON

后端 未结 7 663
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 05:23

I am trying to convert JSON to CSV file, that I can use for further analysis. Issue with my structure is that I have quite some nested dict/lists when I convert my JSON file

7条回答
  •  一向
    一向 (楼主)
    2020-12-03 06:12

    Just pass your dictionary here:

    def getKeyValuePair(dic,master_dic = {},master_key = None):
        keys = list(dic.keys())
        for key in keys:
            if type(dic[key]) == dict:
                    getKeyValuePair(dic[key],master_dic = master_dic,master_key = key)
            else:
                if master_key == None:
                    master_dic[key] = dic[key]
                else:
                    master_dic[str(master_key)+'_'+str(key)] = dic[key]
    
       return master_dic
    

提交回复
热议问题