1

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
6
  • 1
    First of all, you do not have a NESTED json document, the field is merely a string representation of an unevaluated json string. Fix this first and stop using strip or split. Commented May 13, 2015 at 4:08
  • @Anzel - What do you mean by fixing it? and not using string and split? Commented May 13, 2015 at 4:11
  • the field value of message is currently a string. You are dealing with it like string using strip and split, DON'T!. Commented May 13, 2015 at 4:13
  • @Anzel - What is the alternative? Commented May 13, 2015 at 4:14
  • 1
    You need to discard the message string of 'brow_eventdetails:' as this isn't a valid json string (field name without quotes "") so it fails when you do json.loads. Discard it first, like I use replace('brow_eventdetails:', ''), then the remaining string {...} can be parsed by json.loads. Commented May 13, 2015 at 4:31

2 Answers 2

4

You need to parse again the json string of json_data['message'] then just access the desired values, one way to do it:

# since the string value of `message` itself isn't a valid json string
# discard it, and parse it with json again
brow_eventdetails = json.loads(json_data['message'].replace('brow_eventdetails:', ''))

brow_eventdetails['ATime']
Out[6]: 5

brow_eventdetails['SBTime']
Out[7]: 391

brow_eventdetails['CTime']
Out[8]: 425

...
Sign up to request clarification or add additional context in comments.

Comments

1

Parse this string value using json.loads as you would with every other string that contains JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.