将json对象反规范化为平面对象

2024-04-29 07:05:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json对象,比如

 {
        "id": 3590403096656,
        "title": "Romania Special Zip Hoodie Blue - Version 02 A5",
        "tags": [
            "1ST THE WORLD FOR YOU <3",
            "apparel",
        ],
        "props": [
            {
                "id": 28310659235920,
                "title": "S / romainia All Over Print Full Zip Hoodie for Men (Model H14)",
                "position": 1,
                "product_id": 3590403096656,
                "created_at": "2019-05-22T00:46:19+07:00",
                "updated_at": "2019-05-22T01:03:29+07:00"
            },
            {
                "id": 444444444444,
                "title": "number 2",
                "position": 1,
                "product_id": 3590403096656,
                "created_at": "2019-05-22T00:46:19+07:00",
                "updated_at": "2019-05-22T01:03:29+07:00"
            }
        ]
}

我想把它展平,让期望的输出看起来像

{"id": 3590403096656,"title": "Romania Special Zip Hoodie Blue - Version 02 A5","tags": ["1ST THE WORLD FOR YOU <3","apparel"],"props.id": 28310659235920,"props.title": "S / romainia All Over Print Full Zip Hoodie for Men (Model H14)","props.position": 1,"props.product_id": 3590403096656,"props.created_at": "2019-05-22T00:46:19+07:00",       "props.updated_at": "2019-05-22T01:03:29+07:00"}
{"id": 3590403096656,"title": "Romania Special Zip Hoodie Blue - Version 02 A5","tags": ["1ST THE WORLD FOR YOU <3","apparel"],"props.id": 444444444444,"props.title": "number 2","props.position": 1,"props.product_id": 3590403096656,"props.created_at": "2019-05-22T00:46:19+07:00","props.updated_at": "2019-05-22T01:03:29+07:00"}

到目前为止,我已经尝试了:

from pandas.io.json import json_normalize
json_normalize(sample_object)

sample_object包含json对象的地方,我正在循环遍历一个包含此类对象的大文件,我希望以所需的格式将其展平。你知道吗

json_normalize没有给我所需的输出,我想保持标签的原样,但要展平props并重复父对象信息。你知道吗

在这方面的任何帮助都是非常感谢的。你知道吗


Tags: 对象idjsontitlepositionblueproductzip
2条回答

您需要一些json_normalize行为,但需要自定义扭曲。因此,对数据的一部分使用json_normalize或类似的方法,然后将其与数据的其余部分合并。你知道吗

下面的代码更倾向于使用“或类似”路径,到达深度into the pandas codebase以获得nested_to_record帮助函数,该函数将字典展平。它用于创建单独的行,这些行将基础数据(所有属性中通用的键/值)与特定于每个道具条目的平坦数据相结合。有一个注释掉的行在没有nested_to_record的情况下做相同的事情,但是它有点不雅观地变平为DataFrame,然后导出到dict。你知道吗

from collections import OrderedDict
import json
import pandas as pd
from pandas.io.json.normalize import nested_to_record

data = json.loads(rawjson)
props = data.pop('props')
rows = []
for prop in props:
    rowdict = OrderedDict(data)
    flattened_prop = nested_to_record({'props': prop})
    # flatteded_prop = json_normalize({'props': prop}).to_dict(orient='records')[0]
    rowdict.update(flattened_prop)
    rows.append(rowdict)

df = pd.DataFrame(rows)

导致:

output data frame

请试试这个:

import copy

obj =  {
        "id": 3590403096656,
        "title": "Romania Special Zip Hoodie Blue - Version 02 A5",
        "tags": [
            "1ST THE WORLD FOR YOU <3",
            "apparel",
        ],
        "props": [
            {
                "id": 28310659235920,
                "title": "S / romainia All Over Print Full Zip Hoodie for Men (Model H14)",
                "position": 1,
                "product_id": 3590403096656,
                "created_at": "2019-05-22T00:46:19+07:00",
                "updated_at": "2019-05-22T01:03:29+07:00"
            },
            {
                "id": 444444444444,
                "title": "number 2",
                "position": 1,
                "product_id": 3590403096656,
                "created_at": "2019-05-22T00:46:19+07:00",
                "updated_at": "2019-05-22T01:03:29+07:00"
            }
        ]
}

props = obj.pop("props")

for p in props:
    res = copy.deepcopy(obj)
    for k in p:
        res["props."+k] = p[k]
    print(res)

基本上,它使用pop("props")来获取没有"props"的对象(这是所有结果对象中使用的公共部分)

然后我们遍历道具,创建包含基本对象的新对象,然后填充“道具钥匙“每个道具上的每把钥匙。你知道吗

相关问题 更多 >