I have a collection of event logs that I want to access, re-format, and print them in a cleaner, more readable format. An example of such a log would be:
[
{
"_source": {
"message": "<message>",
"tags": [
"winlog",
"2.4.0",
"agents",
"agents_input_codec_plain_applied"
],
"@timestamp": "2023-08-22T15:11:14.146Z",
"observer": {
"ip": "<ip>"
},
"winlog": {
"task": "Account Lockout",
"computer_name": "<name>",
"event_data": {
"IpAddress": "<ip>",
"TargetUserName": "<username>",
"LogonType": "3",
"SubjectUserName": "-",
"TargetDomainName": "<domain>",
"LogonTypeName": "network"
},
"keywords": [
"Audit Failure"
],
"event_id": <id>
},
"log": {
"level": "information"
},
"event": {
"action": "Account Lockout",
"created": "2023-08-22T15:11:15.378Z",
"code": 4625,
"kind": "event"
},
"fields": {
"@timestamp": [
"2023-08-22T15:11:14.146Z"
],
"event.created": [
"2023-08-22T15:11:15.378Z"
]
},
"sort": [
1692717074146
]
},
<next event log>
]
I attempted to open the log file, load the json, and iterate the file, accessing log information, then printing it but I recieve an error and wonder if there is a work around.
with open('logs.json', 'r') as json_file:
logs = json.load(json_file)
for i in logs['event']:
print(i['action'])
Error: list indices must be integers or slices, not str.
for in logs:
print(i)
This code works, but just prints all logs out verbatim.
for i in logs:
print(logs[i]["event"])
This throws the same error as the first example.
Given the specific log format and file, How can I access individual data? Particularly I am trying to access data in winlog and event.