Decoding JSON file using python
Cameron Simpson
cs at zip.com.au
Wed May 27 23:32:39 EDT 2015
On 28May2015 01:38, Jon Ribbens <jon+usenet at unequivocal.co.uk> wrote:
>On 2015-05-27, Karthik Sharma <karthik.sharma at gmail.com> wrote:
>> I tried modifying the program as follows as per your
>> suggestion.Doesn't seem to work.
>
>That's because you didn't modify the program as per their suggestion,
>you made completely different changes that bore no relation to what
>they said.
Actually, his changes looked good to me. He does print from data first, but
only for debugging. Then he goes:
message = json.loads(data)
and tried to access message['Message'].
However I am having trouble reproducing his issue because his quoted code is
incorrect. I've tried to fix it, as listed below, but I don't know what is
really meant to be in the 'data" string.
Hint: Karthik, use raw strings (r'foo') for complicated strings - it avoids a
lot of backslashing etc, and thus avoids errors in backslashing.
Karthik, please correct the code below. Currently I do not get your exception,
I get an exception from the JSON module parsing the "data" string.
Code below,
Cameron Simpson <cs at zip.com.au>
import json
json_input = { "msgType": "0",
"tid": "1",
"data": r'[{"Severity":"warn","Subject":"Reporting ","Message":"tdetails": {"Product":"Gecko","CPUs":8,"Language":"en-GB","isEven t":">}]',
"Timestamp": "1432703193431",
"Host": "myserver.com",
"Agent": "Experience",
"AppName": "undefined",
"AppInstance": "my_server",
"Group": "UndefinedGroup"
}
print('json input original {} \n\n'.format(json_input))
data = json_input['data']
print('data {} \n\n'.format(data))
message = json.loads(data)
print('message {} \n\n'.format(message['Message']))
More information about the Python-list
mailing list