I have some json files in a directory, and I am working on parsing all of the files in said directory. I want to define the columns that the key values will fit under
Currently, the data fits into those columns perfectly. The problem is that my script only takes the first json object from each file instead of parsing through the whole file and returning each indexed json object.
import os, json
import pandas as pd
#Read files from Log directory
path_to_json = r'C:\TestLogfiles'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
print(json_files)
# Define Columns within Log files
jsons_data = pd.DataFrame(columns=['category', 'description', 'created_date', 'host', 'machine', 'user_id', 'error_code', 'process', 'thread'])
# we need both the json and an index number so use enumerate()
for index, js in enumerate(json_files):
with open(os.path.join(path_to_json, js)) as json_files:
json_text = json.load(json_files)
for logMessages in json_text['logMessages']:
#Nav thru the log entry to return a parsed list of these items
category = logMessages['type']
description = logMessages['message']
created_date = logMessages['time']
host = logMessages['source']
machine = logMessages['machine']
user_id = logMessages['user']
error_code = logMessages['code']
process = logMessages['process']
thread = logMessages['thread']
#push list of data into pandas DFrame at a row given by 'index'
jsons_data.loc[index] = [category, description, created_date, host, machine, user_id, error_code, process, thread]
print(jsons_data)
Json files look like this:
{
"hasMore": true,
"startTime": 1663612608354,
"endTime": 1662134983365,
"logMessages": [
{
"type": "DEBUG",
"message": "Health check took 50 ms.",
"time": 1663612608354,
"source": "Portal Admin",
"machine": "machineName.domain.com",
"user": "",
"code": 9999,
"elapsed": "",
"process": "6966",
"thread": "1",
"methodName": "",
"requestID": ""
},
{
"type": "DEBUG",
"message": "Checking the Sharing API took 12 ms.",
"time": 1663612608354,
"source": "Portal Admin",
"machine": "machineName.domain.com",
"user": "",
"code": 9999,
"elapsed": "",
"process": "6966",
"thread": "1",
"methodName": "",
"requestID": ""
}
]
}
Result is:
['PortalLog.json', 'Testlog.json']
category description created_date host machine user_id error_code process thread
0 WARNING Failed to delete service with service URL 'ht... 1662134983365 Sharing MachineName.domain.com portaladmin 200007 11623 1
1 SEVERE Error executing tool. Export Web Map Task : Fa... 1657133904189 Utilities/PrintingTools.GPServer MachineName.domain.com portaladmin 20010 25561 161