I'm writing a log file using python and the log file is like this :
MessageName:mouse left down | TimeStamp:2019-02-13 15:43:31.664 | Window:13500784 | Position:(483, 587) | Wheel:0
MessageName:mouse left up | TimeStamp:2019-02-13 15:43:31.873 | Window:13500784 | Position:(483, 587) | Wheel:0
I would like to convert this log into json format.
This is the code that i have tried :
import json
def convert() :
f = open("log_events.log", "r")
content = f.read()
splitcontent = content.splitlines()
for line in splitcontent :
pipesplit = line.split(' | ')
print(pipesplit)
with open("json_log.json", 'a') as fout:
json.dump(pipesplit, fout, indent=4)
And the Output i need is like this :
[
{
"MessageName" : "mouse left down",
"TimeStamp" : "2019-02-13 15:43:31.664",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : 0"
},
{
"MessageName" : "mouse left up",
"TimeStamp" : "2019-02-13 15:43:31.873",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : "0"
},
]
But with the above given code, my output is
[
"MessageName" : "mouse left down",
"TimeStamp" : "2019-02-13 15:43:31.664",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : 0"
][
"MessageName" : "mouse left up",
"TimeStamp" : "2019-02-13 15:43:31.873",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : "0"
]
How to convert into proper JSON format ?