0

试图将嵌套的 JSON 转换为当前数据框的列。我该如何做到这一点?注意:字典的 JSON 函数列表重复 600 多次

{
"Functions": [
    {
        "CodeSha256": "",
        "CodeSize": 
        "Description": "",
        "Environment": {
            "Variables": {

                "COMMIT_HASH": ",
                "CodeSha256": "",
                "ECS_LOG_STREAM": "",
                "ELASTIC_SEARCH_DOMAIN_ENDPOINT": "",
                "ENVIRONMENT": "prod",
                "SERVICE_NAME": "testingservicename",
                "SERVICE_OWNER": "testingserviceowner",

            }
        },
        
        "FunctionName": "demofunctionname",
        "Timeout": ,
        "TracingConfig": {
            "Mode": 
        },
        "Version": "",
        "VpcConfig": {
            "SecurityGroupIds": [
                ""
            ],
            "SubnetIds": [
                "",
                "",
                ""
            ],
            "VpcId": ""
        }
    }

] }

我如何加载 json

data = json.load(open('../fileservice.json'))
df = pd.DataFrame(data["Functions"])

它是如何出现的

    FunctionName                                    Environment
0 demofunctionname      {Variables{"COMMIT_HASH":"djkdkd","SERVICE_OWNER":"serviceownertest"}}                 

我需要它如何出现

        FunctionName                               COMMIT_HASH       SERVICE_OWNER      
0      demofunctionname                            djkdkd             serviceownertest

一直在尝试爆炸方法,但没有完成工作。非常感谢任何建议或指导。

4

2 回答 2

1

我不认为这是最好的方法。但这是解决方案

s={
    "Functions": [
    {
        "CodeSha256": "",
        "CodeSize": "",
        "Description": "",
        "Environment": {
            "Variables": {
                "COMMIT_HASH": "test",
                "CodeSha256": "",
                "ECS_LOG_STREAM": "",
                "ELASTIC_SEARCH_DOMAIN_ENDPOINT": "",
                "ENVIRONMENT": "prod",
                "SERVICE_NAME": "testingservicename",
                "SERVICE_OWNER": "testingserviceowner",

            }
        },
        
        "FunctionName": "demofunctionname",
        "Timeout": "" ,
        "TracingConfig": {
            "Mode": ""
        },
        "Version": "",
        "VpcConfig": {
            "SecurityGroupIds": [
                ""
            ],
            "SubnetIds": [
                "",
                "",
                ""
            ],
            "VpcId": ""
        }
    }
] }

import pandas as pd
import json

s = json.dumps(s)
data = json.loads(s)

result={'FunctionName': data["Functions"][0]["FunctionName"], 'COMMIT_HASH': data["Functions"][0]["Environment"]["Variables"]["COMMIT_HASH"], 'SERVICE_OWNER': data["Functions"][0]["Environment"]["Variables"]["SERVICE_OWNER"]}

df = pd.DataFrame(data= [result])
print(df)

注意:我认为你的 json 有问题,我已经在我的解决方案中修复了它。

编辑版本

这是多个“功能”的代码

s={
    "Functions": [
        {
            "CodeSha256": "",
            "CodeSize": "",
            "Description": "",
            "Environment": {
                "Variables": {
                    "COMMIT_HASH": "test",
                    "CodeSha256": "",
                    "ECS_LOG_STREAM": "",
                    "ELASTIC_SEARCH_DOMAIN_ENDPOINT": "",
                    "ENVIRONMENT": "prod",
                    "SERVICE_NAME": "testingservicename",
                    "SERVICE_OWNER": "testingserviceowner",
    
                }
            },
            
            "FunctionName": "demofunctionname",
            "Timeout": "" ,
            "TracingConfig": {
                "Mode": ""
            },
            "Version": "",
            "VpcConfig": {
                "SecurityGroupIds": [
                    ""
                ],
                "SubnetIds": [
                    "",
                    "",
                    ""
                ],
                "VpcId": ""
            }
        },
        {
            "CodeSha256": "",
            "CodeSize": "",
            "Description": "",
            "Environment": {
                "Variables": {
                    "COMMIT_HASH": "test",
                    "CodeSha256": "",
                    "ECS_LOG_STREAM": "",
                    "ELASTIC_SEARCH_DOMAIN_ENDPOINT": "",
                    "ENVIRONMENT": "prod",
                    "SERVICE_NAME": "testingservicename",
                    "SERVICE_OWNER": "testingserviceowner",
    
                }
            },
            
            "FunctionName": "demofunctionname 1",
            "Timeout": "" ,
            "TracingConfig": {
                "Mode": ""
            },
            "Version": "",
            "VpcConfig": {
                "SecurityGroupIds": [
                    ""
                ],
                "SubnetIds": [
                    "",
                    "",
                    ""
                ],
                "VpcId": ""
            }
        }
    ]
}

import pandas as pd
import json

s = json.dumps(s)
data = json.loads(s)

function_name_list=[]
commit_hash_list=[]
service_owner_list=[]

for i in range(len(data["Functions"])):
    function_name_list.append(data["Functions"][i]["FunctionName"])
    commit_hash_list.append(data["Functions"][i]["Environment"]["Variables"]["COMMIT_HASH"])
    service_owner_list.append(data["Functions"][i]["Environment"]["Variables"]["SERVICE_OWNER"])

result={'FunctionName': function_name_list, 'COMMIT_HASH': commit_hash_list, 'SERVICE_OWNER': service_owner_list}

df = pd.DataFrame(list(zip(function_name_list, commit_hash_list, service_owner_list)),
               columns =['FunctionName', 'COMMIT_HASH', 'SERVICE_OWNER'])
print(df)
于 2021-07-19T04:12:40.373 回答
1

看看 pd.json_normalize()。这是一个非常好的工具。在你的情况下:

pd.json_normalize(s["Functions"])

将给出以下输出(仅转换后的第一行):

CodeSha256                                                             
CodeSize                                                               
Description                                                            
FunctionName                                           demofunctionname
Timeout                                                                
Version                                                                
Environment.Variables.COMMIT_HASH                                  test
Environment.Variables.CodeSha256                                       
Environment.Variables.ECS_LOG_STREAM                                   
Environment.Variables.ELASTIC_SEARCH_DOMAIN_END...                     
Environment.Variables.ENVIRONMENT                                  prod
Environment.Variables.SERVICE_NAME                   testingservicename
Environment.Variables.SERVICE_OWNER                 testingserviceowner
TracingConfig.Mode                                                     
VpcConfig.SecurityGroupIds                                           []
VpcConfig.SubnetIds                                              [, , ]
VpcConfig.VpcId                                                        
于 2021-07-19T07:14:16.053 回答