Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91348
| Date | 2015-05-28 13:32 +1000 |
|---|---|
| From | Cameron Simpson <cs@zip.com.au> |
| Subject | Re: Decoding JSON file using python |
| References | <slrnmmcsfd.1t4.jon+usenet@frosty.unequivocal.co.uk> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.117.1432785446.5151.python-list@python.org> (permalink) |
On 28May2015 01:38, Jon Ribbens <jon+usenet@unequivocal.co.uk> wrote:
>On 2015-05-27, Karthik Sharma <karthik.sharma@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@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']))
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Decoding JSON file using python Karthik Sharma <karthik.sharma@gmail.com> - 2015-05-27 15:23 -0700
Re: Decoding JSON file using python random832@fastmail.us - 2015-05-27 19:06 -0400
Re: Decoding JSON file using python MRAB <python@mrabarnett.plus.com> - 2015-05-28 00:08 +0100
Re: Decoding JSON file using python Cameron Simpson <cs@zip.com.au> - 2015-05-28 08:52 +1000
Re: Decoding JSON file using python Karthik Sharma <karthik.sharma@gmail.com> - 2015-05-27 16:51 -0700
Re: Decoding JSON file using python Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-28 01:38 +0000
Re: Decoding JSON file using python Cameron Simpson <cs@zip.com.au> - 2015-05-28 13:32 +1000
Re: Decoding JSON file using python Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-28 18:49 +0000
Re: Decoding JSON file using python random832@fastmail.us - 2015-05-27 23:48 -0400
Re: Decoding JSON file using python MRAB <python@mrabarnett.plus.com> - 2015-05-28 04:54 +0100
Re: Decoding JSON file using python Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-28 18:48 +0000
csiph-web