I'm trying to check if there's a username available when type = account
by parsing the key "Entities". If not available, get the IP from type = 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]'
I attempted to use a couple of for loops for data['Entities'][0]
but either getting TypeError: list indices must be integers or slices, not str
or [
. What would be the neat way to access the values in each entity?
for i in data:
print(i['Entities'])