将 json 对象反规范化为平面对象 [英] De-normalize json object into flat objects

查看:48
本文介绍了将 json 对象反规范化为平面对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像 json 对象

<代码> {id":3590403096656,title":Romania Special Zip Hoodie Blue - Version 02 A5",标签":[为您打造世界第一<3",服装",],道具":[{id":28310659235920,title":S/romainia All Over Print Full Zip Hoodie for Men (Model H14)",位置":1,product_id":3590403096656,created_at":2019-05-22T00:46:19+07:00",updated_at":2019-05-22T01:03:29+07:00"},{id":444444444444,标题":数字 2",位置":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",服装"]、props.id":28310659235920、props.title":S/romainia All Over Print Full Zip Hoodie for Men(H14 型)"、props.position":1,props.product_id":3590403096656,props.created_at":2019-05-22T00:46:19+07:00",props.updated_at":2019-05-2029":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,atprops.2"1_d-05-22T00:46:19+07:00","props.updated_at": "2019-05-22T01:03:29+07:00"}

到目前为止我已经尝试过:

 from pandas.io.json import json_normalizejson_normalize(sample_object)

其中 sample_object 包含 json 对象,我正在循环浏览我想以所需格式展平的此类对象的大文件.

json_normalize 没有给我想要的输出,我想按原样保留标签,但扁平化 props 并重复父对象信息.

解决方案

请试试这个:

导入副本对象 = {身份证":3590403096656,"title": "Romania Special Zip Hoodie Blue - Version 02 A5",标签":[为您打造世界第一<3",服饰",],道具": [{身份证":28310659235920,"title": "S/romainia All Over Print Full Zip Hoodie for Men (Model H14)",位置":1,product_id":3590403096656,"created_at": "2019-05-22T00:46:19+07:00","updated_at": "2019-05-22T01:03:29+07:00"},{身份证":444444444444,"title": "number 2",位置":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("道具")对于道具中的 p:res = copy.deepcopy(obj)对于 p 中的 k:res["props."+k] = p[k]打印(资源)

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

然后我们遍历 props,并创建包含基础对象的新对象,然后为每个 props 中的每个键填充props.key".

I have a json object like

 {
        "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"
            }
        ]
}

i want to flatten it so desired output looks like

{"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"}

so far i have tried:

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

where sample_object contains json object, i am looping through a large file of such objects which i want to flatten in desired format.

json_normalize is not giving me desired output, i want to keep tags as it is but flatten props and repeat parent object info.

解决方案

please try this:

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)

basically it use pop("props") to get the obj without "props" (which is the common part to use in all result objects),

then we iterate through props, and create new objects that contain the base object, and then fill "props.key" for every key in every prop.

这篇关于将 json 对象反规范化为平面对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆