用字典列表解析JSON

时间:2020-09-27 19:13:29

标签: python json

我正在尝试通过解析键“实体” 来检查type = account时是否有可用的用户名。如果不可用,请从type = ip

获取IP
data = '[\n    {\n        "TimeRecorded": "2020-09-20T08:56:12Z",\n        "AlertName": "Alert1",\n        "Entities": "[\\r\\n  {\\r\\n    \\"$id\\": \\"3\\",\\r\\n    \\"Address\\": \\"160.160.100.2\\",\\r\\n    \\"Type\\": \\"ip\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"4\\",\\r\\n    \\"DnsDomain\\": \\"example.com\\",\\r\\n    \\"HostName\\": \\"MyMachine1\\",\\r\\n    \\"Type\\": \\"host\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"5\\",\\r\\n    \\"Name\\": \\"Tenant1\\",\\r\\n    \\"Type\\": \\"account\\"\\r\\n  }\\r\\n]"\n    },\n    {\n        "TimeRecorded": "2020-09-20T07:56:13Z",\n        "AlertName": "Alert2",\n        "Entities": "[\\r\\n  {\\r\\n    \\"$id\\": \\"3\\",\\r\\n    \\"Address\\": \\"160.160.100.1\\",\\r\\n    \\"Type\\": \\"ip\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"4\\",\\r\\n    \\"DnsDomain\\": \\"example.com\\",\\r\\n    \\"HostName\\": \\"MyMachine2\\",\\r\\n    \\"Type\\": \\"host\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"5\\",\\r\\n    \\"Name\\": \\"Tenant2\\",\\r\\n    \\"Type\\": \\"account\\"\\r\\n  }\\r\\n]"\n    },\n    {\n        "TimeRecorded": "2020-09-20T05:56:14Z",\n        "AlertName": "Alert3",\n        "Entities": "[\\r\\n  {\\r\\n    \\"$id\\": \\"3\\",\\r\\n    \\"Address\\": \\"160.160.100.3\\",\\r\\n    \\"Type\\": \\"ip\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"4\\",\\r\\n    \\"DnsDomain\\": \\"example.com\\",\\r\\n    \\"HostName\\": \\"MyMachine3\\",\\r\\n    \\"Type\\": \\"host\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"5\\",\\r\\n    \\"Name\\": \\"Tenant3\\",\\r\\n    \\"Type\\": \\"account\\"\\r\\n  }\\r\\n]"\n    }\n]'

我尝试为data['Entities'][0]使用几个for循环,但得到TypeError: list indices must be integers or slices, not str[。在每个实体中访问值的整洁方法是什么?

for i in data:
     print(i['Entities'])

3 个答案:

答案 0 :(得分:1)

您可以使用json.loads()两次解析此结构奇特的JSON。

import json
from pprint import pprint

newdata = json.loads(data) # creates dict with some embedded JSON
for alert in newdata:
    alert["Entities"] = json.loads(alert["Entities"])

pprint(newdata)

收益

[{'AlertName': 'Alert1',
  'Entities': [{'$id': '3', 'Address': '160.160.100.2', 'Type': 'ip'},
               {'$id': '4',
                'DnsDomain': 'example.com',
                'HostName': 'MyMachine1',
                'Type': 'host'},
               {'$id': '5', 'Name': 'Tenant1', 'Type': 'account'}],
  'TimeRecorded': '2020-09-20T08:56:12Z'},
 {'AlertName': 'Alert2',
  'Entities': [{'$id': '3', 'Address': '160.160.100.1', 'Type': 'ip'},
               {'$id': '4',
                'DnsDomain': 'example.com',
                'HostName': 'MyMachine2',
                'Type': 'host'},
               {'$id': '5', 'Name': 'Tenant2', 'Type': 'account'}],
  'TimeRecorded': '2020-09-20T07:56:13Z'},
 {'AlertName': 'Alert3',
  'Entities': [{'$id': '3', 'Address': '160.160.100.3', 'Type': 'ip'},
               {'$id': '4',
                'DnsDomain': 'example.com',
                'HostName': 'MyMachine3',
                'Type': 'host'},
               {'$id': '5', 'Name': 'Tenant3', 'Type': 'account'}],
  'TimeRecorded': '2020-09-20T05:56:14Z'}]

请注意,"$id"字段中的数字存储为文本,因此,如果需要这些值,则需要在其上使用int()

答案 1 :(得分:0)

只需遍历php artisan vendor:publish --tag=jetstream-views (假设它已经是JSON形式),并在每次迭代时使用data处理实体密钥。 (目前,您的数据为字符串形式)

json.loads

答案 2 :(得分:0)

您的数据以一种非常不寻常且麻烦的方式进行格式化_实际上,它是JSON格式的 不是 ,而是Python数据结构的字符串文字表示形式-但是可以使用ast.lister_eval() 两次解决此问题,如下所示。从输出中应该可以看到,每个alert的内容都无法通过字典列表中的键值进行访问。

from ast import literal_eval
from pprint import pprint


data = '[\n    {\n        "TimeRecorded": "2020-09-20T08:56:12Z",\n        "AlertName": "Alert1",\n        "Entities": "[\\r\\n  {\\r\\n    \\"$id\\": \\"3\\",\\r\\n    \\"Address\\": \\"160.160.100.2\\",\\r\\n    \\"Type\\": \\"ip\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"4\\",\\r\\n    \\"DnsDomain\\": \\"example.com\\",\\r\\n    \\"HostName\\": \\"MyMachine1\\",\\r\\n    \\"Type\\": \\"host\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"5\\",\\r\\n    \\"Name\\": \\"Tenant1\\",\\r\\n    \\"Type\\": \\"account\\"\\r\\n  }\\r\\n]"\n    },\n    {\n        "TimeRecorded": "2020-09-20T07:56:13Z",\n        "AlertName": "Alert2",\n        "Entities": "[\\r\\n  {\\r\\n    \\"$id\\": \\"3\\",\\r\\n    \\"Address\\": \\"160.160.100.1\\",\\r\\n    \\"Type\\": \\"ip\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"4\\",\\r\\n    \\"DnsDomain\\": \\"example.com\\",\\r\\n    \\"HostName\\": \\"MyMachine2\\",\\r\\n    \\"Type\\": \\"host\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"5\\",\\r\\n    \\"Name\\": \\"Tenant2\\",\\r\\n    \\"Type\\": \\"account\\"\\r\\n  }\\r\\n]"\n    },\n    {\n        "TimeRecorded": "2020-09-20T05:56:14Z",\n        "AlertName": "Alert3",\n        "Entities": "[\\r\\n  {\\r\\n    \\"$id\\": \\"3\\",\\r\\n    \\"Address\\": \\"160.160.100.3\\",\\r\\n    \\"Type\\": \\"ip\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"4\\",\\r\\n    \\"DnsDomain\\": \\"example.com\\",\\r\\n    \\"HostName\\": \\"MyMachine3\\",\\r\\n    \\"Type\\": \\"host\\"\\r\\n  },\\r\\n  {\\r\\n    \\"$id\\": \\"5\\",\\r\\n    \\"Name\\": \\"Tenant3\\",\\r\\n    \\"Type\\": \\"account\\"\\r\\n  }\\r\\n]"\n    }\n]'

data = literal_eval(data)
for alert in data:
    alert['Entities'] = literal_eval(alert['Entities'])

pprint(data, sort_dicts=False)

输出:

[{'TimeRecorded': '2020-09-20T08:56:12Z',
  'AlertName': 'Alert1',
  'Entities': [{'$id': '3', 'Address': '160.160.100.2', 'Type': 'ip'},
               {'$id': '4',
                'DnsDomain': 'example.com',
                'HostName': 'MyMachine1',
                'Type': 'host'},
               {'$id': '5', 'Name': 'Tenant1', 'Type': 'account'}]},
 {'TimeRecorded': '2020-09-20T07:56:13Z',
  'AlertName': 'Alert2',
  'Entities': [{'$id': '3', 'Address': '160.160.100.1', 'Type': 'ip'},
               {'$id': '4',
                'DnsDomain': 'example.com',
                'HostName': 'MyMachine2',
                'Type': 'host'},
               {'$id': '5', 'Name': 'Tenant2', 'Type': 'account'}]},
 {'TimeRecorded': '2020-09-20T05:56:14Z',
  'AlertName': 'Alert3',
  'Entities': [{'$id': '3', 'Address': '160.160.100.3', 'Type': 'ip'},
               {'$id': '4',
                'DnsDomain': 'example.com',
                'HostName': 'MyMachine3',
                'Type': 'host'},
               {'$id': '5', 'Name': 'Tenant3', 'Type': 'account'}]}]