Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91347

Re: Decoding JSON file using python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:\\ 20': 0.05; 'indices': 0.07; 'json': 0.07; 'line:': 0.07; 'modifying': 0.07; 'skip:\\ 10': 0.07; 'subject:file': 0.07; 'skip:\\ 30': 0.09; 'subject:using': 0.09; 'typeerror:': 0.09; 'python': 0.11; 'thursday,': 0.13; 'subject:python': 0.14; 'represents': 0.15; '23,': 0.16; 'cjson': 0.16; 'decode': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'integers,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'simpson': 0.16; 'skip:{ 40': 0.16; 'structure.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'input': 0.18; 'skip:" 40': 0.20; '2015': 0.23; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; '(most': 0.24; 'example': 0.25; 'header:User-Agent:1': 0.26; 'error': 0.27; 'follows': 0.29; 'str': 0.29; 'work.': 0.30; 'error.': 0.31; 'code': 0.31; 'skip:d 20': 0.32; 'says': 0.32; 'received:84': 0.32; 'structure': 0.32; 'getting': 0.33; 'traceback': 0.33; 'this?': 0.34; 'file': 0.34; 'to:addr:python- list': 0.35; 'list': 0.35; 'data.': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'to:addr:python.org': 0.39; 'seem': 0.39; 'received:192': 0.39; 'data': 0.40; 'your': 0.60; 'per': 0.61; 'different': 0.64; 'cameron': 0.66; '\\n\\n': 0.84; 'dict.': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=cblcrxzM c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=20eAzwmGAAAA:8 a=pGLkceISAAAA:8 a=g8KEX7M-AAAA:8 a=NksUtzOUAawTnrB6KZUA:9 a=_oGLMtU26343XR_L:21 a=wGnFo3MzmXfVqgdy:21 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Thu, 28 May 2015 04:54:23 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Decoding JSON file using python
References <11dbf982-efc1-4ef0-b286-e3d08ba5030e@googlegroups.com> <mailman.108.1432768474.5151.python-list@python.org> <75320b35-72b6-4aae-a47f-630bd7bef812@googlegroups.com>
In-Reply-To <75320b35-72b6-4aae-a47f-630bd7bef812@googlegroups.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.116.1432785275.5151.python-list@python.org> (permalink)
Lines 89
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432785275 news.xs4all.nl 2877 [2001:888:2000:d::a6]:52057
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91347

Show key headers only | View raw


On 2015-05-28 00:51, Karthik Sharma wrote:
> I tried modifying the program as follows as per your suggestion.Doesn't seem to work.
>
> import simplejson as json
> import cjson
>
> 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"
> }
>
> 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']))
>
> I get the following error.
>
> Traceback (most recent call last):
>    File "test_json.py", line 23, in <module>
>      print('message {} \n\n'.format(message['Message']))
> TypeError: list indices must be integers, not str
> karthik.sharma@aukksharma2:~$ vim test_json.py
>
You have a different error message now. It says that 'message' is a
list.

Have a look at the JSON data. It represents a list that contains a dict.

>
> On Thursday, 28 May 2015 11:14:44 UTC+12, Cameron Simpson  wrote:
>> 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']
>>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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