Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91334
| Date | 2015-05-28 08:52 +1000 |
|---|---|
| From | Cameron Simpson <cs@zip.com.au> |
| Subject | Re: Decoding JSON file using python |
| References | <11dbf982-efc1-4ef0-b286-e3d08ba5030e@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.108.1432768474.5151.python-list@python.org> (permalink) |
On 27May2015 15:23, Karthik Sharma <karthik.sharma@gmail.com> wrote:
>I have the JSON structure shown below and the python code shown below to manipulate the JSON structure.
>
> import json
>
> json_input = {
> "msgType": "0",
> "tid": "1",
> "data": "[{\"Severity\":\"warn\",\"Subject\":\"Reporting \",\"Message\":\"tdetails:{\\\"Product\\\":\\\"Gecko\\\",\\\"CPUs\\\":8,\\\"Language\\\":\\\"en-GB\\\",\\\"isEvent\\\":\\\">
> "Timestamp": "1432703193431",
> "Host": "myserver.com",
> "Agent": "Experience",
> "AppName": "undefined",
> "AppInstance": "my_server",
> "Group": "UndefinedGroup"
> }
>
>
> data = json_input['data']
> tdetails = data['Message']
> print('json_input {} \n\ndata {} \n\n tdetails {}\n\n'.format(json_input,data,tdetails))
>
>I am getting the error.
>
> Traceback (most recent call last):
> File "test_json.py", line 19, in <module>
> tdetails = data['Message']
> TypeError: string indices must be integers, not str
That will be because of this line:
tdetails = data['Message']
because "data" is just the string from your dict.
>The JSON structure is valid as shown by http://jsonlint.com/
> I want to be able to access the different fields inside `data` such as `severity`, `subject` and also fields inside `tdetails` such as `CPUs` and `Product`. How do I do this?
Then you need to decode "data". Example (untested):
data_decoded = json.loads(data)
and then access:
data_decoded['Message']
Cheers,
Cameron Simpson <cs@zip.com.au>
Here's a great .sig I wrote, so good it doesn't rhyme.
Jon Benger <jbenger@agravaine.st.nepean.uws.edu.au>
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