I have the following JSON structure. I am attempting to extract the following information from the "brow_eventdetails" section.
- ATime
- SBTime
- CTime
My question is is there any easy way to do this without using regular expression. In other words my question is the a nested JSON format that I can extract by some means using python.
{
"AppName": "undefined",
"Event": "browser information event",
"Message": "brow_eventdetails:{\"Message\":\"for https://mysite.myspace.com/display/CORE/mydetails took too long (821 ms : ATime: 5 ms, SBTime: 391 ms, CTime: 425 ms), and exceeded threshold of 5 ms\",\"Title\":\"mydetails My Work Details\",\"Host\":\"nzmyserver.ad.mydomain.com\",\"Page URL\":\"https://nzmyserver.mydomain.com/display/CORE/mydetails\",\"PL\":821,\"ATime\":5,\"SBTime\":391,\"CTime\":425}",
"Severity": "warn",
"UserInfo": "General Info"
}
The program that I use is given below.
with open(fname, 'r+') as f:
json_data = json.load(f)
message = json_data['Message']
nt = message.split('ATime')[1].strip().split(':')[1].split(',')[0]
bt = message.split('SBTime')[1].strip().split(':')[1].split('\s')[0])
st = message.split('CTime')[1].strip().split(':')[1].split('\s')[0])
json_data["ATime"] = bt
json_data["SBTime"] = st
json_data["CTime"] = nt
f.seek(0)
json.dump(json_data,f,ensure_ascii=True)
There are some issues with this program.The first one is extracting ATime,SBTime and CTime. These values are repeated.I want to extract just the numeric values, 5, 391 and 425.I don't want ms that follows it.how can I achieve this?
If I were to update the program to use json.loads() as below I get the following error
with open(fname, 'r+') as f: json_data = json.load(f) message = json_data['Message'] message_data = json.loads(message) f.seek(0) json.dump(json_data,f,ensure_ascii=True)
I get
ValueError: No JSON object could be decoded
striporsplit.messageis currently a string. You are dealing with it like string usingstripandsplit, DON'T!.messagestring of'brow_eventdetails:'as this isn't a valid json string (field name without quotes"") so it fails when you dojson.loads. Discard it first, like I usereplace('brow_eventdetails:', ''), then the remaining string{...}can be parsed byjson.loads.