flatten_json 列表的递归展平函数

我想在每个级别展平以下 JSON 并为每个级别创建一个 pandas 数据框,flatten_json我曾经这样做过,但为此我需要遍历每个级别,从而创建多个嵌套的 for 循环:


{

"metadata": {

    "name": "abc",

    "time": "2020-04-01"

},

"data": [

    {

        "identifiers": [

            {

                "type": "abc",

                "scheme": "def",

                "value": "123"

            },

            {

                "type": "abc",

                "scheme": "def",

                "value": "123"

            }

        ],

        "name": "qwer",

        "type": "abd",

        "level1": [

            {

                "identifiers": [

                    {

                        "type": "abc",

                        "scheme": "def",

                        "value": "123"

                    },

                    {

                        "type": "abc",

                        "scheme": "def",

                        "value": "123"

                    }

                ],

                "name": "asd",

                "type": "abd",

                "level2": [

                    {

                        "identifiers": [

                            {

                                "type": "abc",

                                "scheme": "def",

                                "value": "123"

                            },

                            {

                                "type": "abc",

                                "scheme": "def",

                                "value": "123"

                            }

                        ],

                        "name": "abs",

                        "type": "abd"

                    },

         

慕娘9325324
浏览 103回答 1
1回答

收到一只叮咚

我使用递归解决了它,这是我的代码:import jsonimport pandas as pdimport flatten_json as fjkeys = {'data', 'level1', 'level2', 'level3'}with open('test_lh.json') as f:&nbsp; &nbsp; data = json.load(f)levels = ['data.level1.level2.level3', 'data.level1.level2', 'data.level1', 'data']recs_dict = {}def do_step(data_dict, level, depth, path):&nbsp; &nbsp; recs = []&nbsp; &nbsp; for x in data_dict[level]:&nbsp; &nbsp; &nbsp; &nbsp; if depth < len(path.split('.'))-1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do_step(x, path.split('.')[depth+1], depth+1, path)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dic = fj.flatten(x, root_keys_to_ignore=keys)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recs.append(dic)&nbsp; &nbsp; recs_dict[level] = recsfor path in levels:&nbsp; &nbsp; do_step(data, path.split('.')[0], 0, path)for key, value in recs_dict.items():&nbsp; &nbsp; print(key)&nbsp; &nbsp; df = pd.DataFrame(recs_dict[key])&nbsp; &nbsp; print(df)这是输出:level3&nbsp; identifiers_0_type identifiers_0_scheme identifiers_0_value identifiers_1_type identifiers_1_scheme identifiers_1_value name&nbsp; &nbsp; type0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; abs&nbsp; level31&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; abs&nbsp; level3level2&nbsp; identifiers_0_type identifiers_0_scheme identifiers_0_value identifiers_1_type identifiers_1_scheme identifiers_1_value name&nbsp; &nbsp; type0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; abs&nbsp; level21&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; abs&nbsp; &nbsp; &nbsp;abdlevel1&nbsp; identifiers_0_type identifiers_0_scheme identifiers_0_value identifiers_1_type identifiers_1_scheme identifiers_1_value name&nbsp; &nbsp; type0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; asd&nbsp; level1data&nbsp; identifiers_0_type identifiers_0_scheme identifiers_0_value identifiers_1_type identifiers_1_scheme identifiers_1_value&nbsp; name type0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;123&nbsp; qwer&nbsp; abd
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python