# How to read audit.log json format file (mainly audit\_record details) using python?

**URL:** <https://forums.percona.com/t/how-to-read-audit-log-json-format-file-mainly-audit-record-details-using-python/19442>\
**Category:** MySQL & MariaDB\
**Tags:** community, mysql, percona\
**Created:** [January 3, 2023, 1:40pm UTC](https://forums.percona.com/t/how-to-read-audit-log-json-format-file-mainly-audit-record-details-using-python/19442 "2023-01-03T13:40:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitika\_Rawat](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nitika\_Rawat](https://forums.percona.com/u/Nitika_Rawat)\
**Post date:** [January 3, 2023, 1:40pm UTC](https://forums.percona.com/t/how-to-read-audit-log-json-format-file-mainly-audit-record-details-using-python/19442/1 "2023-01-03T13:40:50Z")

</div>

Hello, I want to read audit.log file (json format) using python to get the audit\_record details but json.load or json.loads is not for for it.  
Any suggestions?

---

<div class="post-metadata">

**Author:** ![Abhinav\_Gupta](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/abhinav_gupta/32/6262_2.png) [@Abhinav\_Gupta](https://forums.percona.com/u/Abhinav_Gupta)\
**Post date:** [January 3, 2023, 2:43pm UTC](https://forums.percona.com/t/how-to-read-audit-log-json-format-file-mainly-audit-record-details-using-python/19442/2 "2023-01-03T14:43:37Z")

</div>

Hi Nitika,

I don’t think there should be any issue with parsing the audit log(json format) content using Python.

Just a simple example I am sharing below:

Sample Audit.log File Content:

```auto
{"audit_record":{"name":"Query","record":"13149_2021-06-30T15:03:11","timestamp":"2021-06-30T15:07:58 UTC","command_class":"show_databases","connection_id":"2","status":0,"sqltext":"show databases","user":"root[root] @ localhost []","host":"localhost","os_user":"mysql","ip":"1.2.3.4","db":"testing"}}

```

Python Code:

```auto
import json

# Open the audit log file in read mode
with open('Audit.log', 'r') as f:
  # Use the json.load() function to parse the contents of the file into a dictionary
  audit_log = json.load(f)

# Access the contents of the dictionary using keys and values
for key1, value1 in audit_log.items():
  print(key1, value1)
  for key2, value2 in value1.items():
    print(key2, value2)

```

Result:

```auto
audit_record {'name': 'Query', 'record': '13149_2021-06-30T15:03:11', 'timestamp': '2021-06-30T15:07:58 UTC', 'command_class': 'show_databases', 'connection_id': '2', 'status': 0, 'sqltext': 'show databases', 'user': 'root[root] @ localhost []', 'host': 'localhost', 'os_user': 'mysql', 'ip': '1.2.3.4', 'db': 'testing'}
name Query
record 13149_2021-06-30T15:03:11
timestamp 2021-06-30T15:07:58 UTC
command_class show_databases
connection_id 2
status 0
sqltext show databases
user root[root] @ localhost []
host localhost
os_user mysql
ip 1.2.3.4
db testing

```
