从json提取字段

时间:2018-12-14 08:11:11

标签: python json pandas

"Repo": {
    "D_Repo": {
        "fields": [
            {
                "Out": " ",
                "number": {
                    "Bks": "[number is valid]",
                    "Ban": "[VALID]"
                },
                "st_ID": 2
                "sign": "No Match"
            }
        ],
        "RID": 2546,
        "result": "OK"
    }

我已经使用pd.read_json读取了json,并且Repo是数据框中的一列。因此,如何访问嵌套列“ bks,Ban,Out,RID和Decision”,像这样,“ info”中就有70条记录。在上面的代码中,我只显示了一条记录。有人可以提供逻辑和代码段帮助吗?

1 个答案:

答案 0 :(得分:1)

希望这会有所帮助!! (假设d是您所拥有的)

   d = {"Repo": {
        "DE_Repo": {
            "info": [
                {
                    "Out": " ",
                    "Value": {
                        "Bks": "[number is valid]",
                        "Ban": "[VALID]"
                    },
                    "ID": 2,
                    "Remark": "No Match"
                },
                {
                    "Out": " ",
                    "Value": {
                        "Bks": "[number is valid]",
                        "Ban": "[VALID]"
                    },
                    "ID": 2,
                    "Remark": "No Match"
                }
            ],
            "RID": 2546,
            "Decision": "Approved"
        }}}


repo_data = d['Repo']['DE_Repo']

values = []



 for keys, val in repo_data.items():
    if type(val) == list:
        for i in range(len(val)):
            for key,data in val[i].items():
                if type(data) == dict:
                    for subKey, subValue in data.items():
                        values.append([subKey,subValue])
                else:
                    values.append([key,data])
    else:
        values.append([keys,val])





extracted_points = pd.DataFrame(values,columns=['Key','Value'])

输出

           0                  1
0        Out                   
1        Bks  [number is valid]
2        Ban            [VALID]
3         ID                  2
4     Remark           No Match
5        Out                   
6        Bks  [number is valid]
7        Ban            [VALID]
8         ID                  2
9     Remark           No Match
10       RID               2546
11  Decision           Approved