使用未知键和类型解析 JSON [python]

parse JSON with unknown keys and types [python]

提问人:Effie 提问时间:10/6/2023 最后编辑:Effie 更新时间:10/6/2023 访问量:42

问:

我正在尝试读取从 AWS API 获得的任何 json 响应。例如,ec2_client。describe_network_interfaces()。 在 https://stackoverflow.com/a/52414034/17750094 的帮助下,我设法创建了一个包含所有级别上所有键值对的字典。但是有没有更好的方法可以做到这一点呢?

到目前为止我的代码:

def read_dict(dato):
    key_value = {}
    for keys, v in show_indices(dato, []):
        key = ".".join(map(str, keys))
        key_value.update({str(key): v})
    return key_value


def show_indices(dato, indices):
    for k, v in dato.items() if isinstance(dato, dict) else enumerate(dato):
        if isinstance(v, (dict, list)):
            yield from show_indices(v, indices + [k])
        else:
            yield indices + [k], v

if __name__ == '__main__':
    ec2 = boto3.client('ec2')
    data = ec2.describe_network_interfaces()

    list_of_dicts = []

    for d in data:
        list_of_dicts.append(read_dict(d))

json 数据:

data = [
   {
      "Attachment":{
         "AttachTime":datetime.datetime(2023,3,2,1,2,2,"tzinfo=tzutc())",
         "AttachmentId":"eni-attach-123",
         "DeleteOnTermination":false,
         "DeviceIndex":1,
         "NetworkCardIndex":0,
         "InstanceOwnerId":"123",
         "Status":"attached"
      },
      "AvailabilityZone":"us-east-5g",
      "Description":"AWS network interface",
      "Groups":[
         {
            "GroupName":"d-123",
            "GroupId":"sg-123"
         }
      ],
      "InterfaceType":"interface",
      "Ipv6Addresses":[
         
      ],
      "MacAddress":"c5:d5:ad:3b:f5:1d",
      "NetworkInterfaceId":"eni-123",
      "OwnerId":"123",
      "PrivateIpAddress":"1.1.1.1",
      "PrivateIpAddresses":[
         {
            "Primary":true,
            "PrivateIpAddress":"1.1.1.1"
         }
      ],
      "RequesterId":"123",
      "RequesterManaged":true,
      "SourceDestCheck":true,
      "Status":"in-use",
      "SubnetId":"subnet-123",
      "TagSet":[
         
      ],
      "VpcId":"vpc-123"
   },
   {
      "Attachment":{
         "AttachTime":datetime.datetime(2023,3,2,1,2,2,"tzinfo=tzutc())",
         "AttachmentId":"eni-attach-123",
         "DeleteOnTermination":false,
         "DeviceIndex":1,
         "NetworkCardIndex":0,
         "InstanceOwnerId":"123",
         "Status":"attached"
      },
      "AvailabilityZone":"us-east-1f",
      "Description":"AWS network interface",
      "Groups":[
         {
            "GroupName":"d-123",
            "GroupId":"sg-123"
         }
      ],
      "InterfaceType":"interface",
      "Ipv6Addresses":[
         
      ],
      "MacAddress":"25:fd:ff:e2:35:c2",
      "NetworkInterfaceId":"eni-123",
      "OwnerId":"123",
      "PrivateIpAddress":"1.1.1.1",
      "PrivateIpAddresses":[
         {
            "Primary":true,
            "PrivateIpAddress":"1.1.1.1"
         }
      ],
      "RequesterId":"123",
      "RequesterManaged":true,
      "SourceDestCheck":true,
      "Status":"in-use",
      "SubnetId":"subnet-123",
      "TagSet":[
         
      ],
      "VpcId":"vpc-123"
   }
]

输出为字典列表:

[
   {
      "Attachment.AttachTime":datetime(2023,3,2,1,2,2,"tzinfo=tzutc())",
      "Attachment.AttachmentId":"eni-attach-123",
      "Attachment.DeleteOnTermination":false,
      "Attachment.DeviceIndex":1,
      "Attachment.NetworkCardIndex":0,
      "Attachment.InstanceOwnerId":"123",
      "Attachment.Status":"attached",
      "AvailabilityZone":"us-east-5g",
      "Description":"AWS network interface",
      "Groups.0.GroupName":"d-123",
      "Groups.0.GroupId":"sg-123",
      "InterfaceType":"interface",
      "MacAddress":"c5:d5:ad:3b:f5:1d",
      "NetworkInterfaceId":"eni-123",
      "OwnerId":"123",
      "PrivateIpAddress":"1.1.1.1",
      "PrivateIpAddresses.0.Primary":true,
      "PrivateIpAddresses.0.PrivateIpAddress":"1.1.1.1",
      "RequesterId":"123",
      "RequesterManaged":true,
      "SourceDestCheck":true,
      "Status":"in-use",
      "SubnetId":"subnet-123",
      "VpcId":"vpc-123"
   },
   {
      "Attachment.AttachTime":datetime.datetime(2023,3,2,1,2,2,"tzinfo=tzutc())",
      "Attachment.AttachmentId":"eni-attach-123",
      "Attachment.DeleteOnTermination":false,
      "Attachment.DeviceIndex":1,
      "Attachment.NetworkCardIndex":0,
      "Attachment.InstanceOwnerId":"123",
      "Attachment.Status":"attached",
      "AvailabilityZone":"us-east-1f",
      "Description":"AWS network interface",
      "Groups.0.GroupName":"d-123",
      "Groups.0.GroupId":"sg-123",
      "InterfaceType":"interface",
      "MacAddress":"25:fd:ff:e2:35:c2",
      "NetworkInterfaceId":"eni-123",
      "OwnerId":"123",
      "PrivateIpAddress":"1.1.1.1",
      "PrivateIpAddresses.0.Primary":true,
      "PrivateIpAddresses.0.PrivateIpAddress":"1.1.1.1",
      "RequesterId":"123",
      "RequesterManaged":true,
      "SourceDestCheck":true,
      "Status":"in-use",
      "SubnetId":"subnet-123",
      "VpcId":"vpc-123"
   }
]

输出将写入 csv 文件。

但是有没有更好的方法可以做到这一点呢? 例如,我宁愿不要保留每个级别的所有密钥。不保留列表索引。

而不是

"Groups.0.GroupName":"d-123",

我想要

"GroupName":"d-123", 
JSON的 列表 字典

评论

1赞 Nick 10/6/2023
但是,如果你有这样的东西呢?{ "Product" : { "Name" : "XYZ" }, "Customer" : { "Name" : "PQR" } }

答:

2赞 Paramjeet 10/6/2023 #1

我不确定你是否想使用临时逻辑来解决它,但如果你只是想解决问题,那么有一个 python 包要好得多。您可以在此处访问该软件包 https://pypi.org/project/flatten-json/