Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 展平深度嵌套的JSON垂直转换为熊猫_Python_Pandas - Fatal编程技术网

Python 展平深度嵌套的JSON垂直转换为熊猫

Python 展平深度嵌套的JSON垂直转换为熊猫,python,pandas,Python,Pandas,您好,我正在尝试展平JSON文件,但无法。我的JSON有3个缩进重复示例,如下所示 floors": [ { "uuid": "8474", "name": "some value", "areas": [ { "uuid": "xyz", "**name**": "qwe",

您好,我正在尝试展平JSON文件,但无法。我的JSON有3个缩进重复示例,如下所示

floors": [
        {
            "uuid": "8474",
            "name": "some value",
            "areas": [
                {
                    "uuid": "xyz",
                    "**name**": "qwe",
                    "roomType": "Name1",
                    "templateUuid": "sdklfj",
                    "templateName": "asdf",
                    "templateVersion": "2.7.1",
                    "Required1": [
                        {
                            "**uuid**": "asdf",
                            "description": "asdf3",
                            "categoryName": "asdf",
                            "familyName": "asdf",
                            "productName": "asdf3",
                            "Required2": [
                                {
                                    "**deviceId**": "asdf",
                                    "**deviceUuid**": "asdf-asdf"
                                }
                            ]
                        }
我想要嵌套Required1中对应的区域值和Required1对应的required 2中对应的区域值。(在**中突出显示) 我尝试了JSON规范化,如下所示,但失败,其他免费LIB:

尝试: 没有显示区域值。仅需要1个需要2个仍然嵌套
希望获得以下值: 预期列数: 楼层.区域.需要1.需要2.设备UUID 楼层、区域、名称 (并排)


如果我在尝试中遗漏了什么,请帮助我。我对JSON加载相当陌生。

假设以下JSON(正如许多人指出的,它是不完整的)。所以我根据你的支架开口完成了它

dct = {"floors": [
        {
            "uuid": "8474",
            "name": "some value",
            "areas": [
                {
                    "uuid": "xyz",
                    "name": "qwe",
                    "roomType": "Name1",
                    "templateUuid": "sdklfj",
                    "templateName": "asdf",
                    "templateVersion": "2.7.1",
                    "Required1": [
                        {
                            "uuid": "asdf",
                            "description": "asdf3",
                            "categoryName": "asdf",
                            "familyName": "asdf",
                            "productName": "asdf3",
                            "Required2": [
                                {
                                    "deviceId": "asdf",
                                    "deviceUuid": "asdf-asdf"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
]}
您可以执行以下操作(需要0.25.0)

这就给了,

>>>     floors.areas.name   uuid    deviceId    deviceUuid
>>> 0   qwe asdf    asdf    asdf-asdf

您的初始json有一些错误,您能纠正它吗?嗨,DataNearoor,{“floor::{}”,val1::{“areas::{“val2::{}}”,Devices::{“val3:::{}}val1嵌套在val2中,val3中嵌套在val2中。需要将所有内容都放在一行中,下一行则是添加新值并垂直向下移动,您可以使用预期的输出进行更新吗?表中包含以下列:name | uuid | deviceId | deviceUuid@user3280146Hi@thushv89,感谢您的回复,并对延迟确认您的解决方案表示抱歉。不幸的是,如果有两级嵌套,那么这个解决方案工作得很好。假设所需的2个标记有多个条目。你有没有一个解决方案?下面的评论有一个例子。假设一个必需的1“Required2”有许多必需的2:[{“deviceId”:“asdf”,“deviceUuid”:“asdf asdf”},{“deviceId”:“bhgk”,“deviceUuid”:“bhgk bhgk”}]@superetbose,您能用示例和所需输出编辑您的问题吗?
from flatten_json import flatten_json
Flat_J1= pd.DataFrame([flatten_json(data_item)]) 
dct = {"floors": [
        {
            "uuid": "8474",
            "name": "some value",
            "areas": [
                {
                    "uuid": "xyz",
                    "name": "qwe",
                    "roomType": "Name1",
                    "templateUuid": "sdklfj",
                    "templateName": "asdf",
                    "templateVersion": "2.7.1",
                    "Required1": [
                        {
                            "uuid": "asdf",
                            "description": "asdf3",
                            "categoryName": "asdf",
                            "familyName": "asdf",
                            "productName": "asdf3",
                            "Required2": [
                                {
                                    "deviceId": "asdf",
                                    "deviceUuid": "asdf-asdf"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
]}
df = pd.io.json.json_normalize(
    dct, record_path=['floors','areas', 'Required1'],meta=[['floors', 'areas', 'name']])
df = df.explode('Required2')
df = pd.concat([df, df["Required2"].apply(pd.Series)], axis=1)
df = df[['floors.areas.name', 'uuid', 'deviceId', 'deviceUuid']]
>>>     floors.areas.name   uuid    deviceId    deviceUuid
>>> 0   qwe asdf    asdf    asdf-asdf