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


Groups > comp.lang.python > #91334

Re: Decoding JSON file using python

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'indices': 0.07; 'json': 0.07; 'line:': 0.07; 'subject:file': 0.07; 'subject:using': 0.09; 'typeerror:': 0.09; 'python': 0.11; 'subject:python': 0.14; 'decode': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'integers,': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'simpson': 0.16; 'skip:{ 40': 0.16; 'structure.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'skip:" 40': 0.20; 'cheers,': 0.24; 'import': 0.24; 'header:In-Reply- To:1': 0.24; '(most': 0.24; 'example': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.28; 'str': 0.29; 'error.': 0.31; 'code': 0.31; 'skip:d 20': 0.32; 'structure': 0.32; 'getting': 0.33; 'traceback': 0.33; 'this?': 0.34; 'file': 0.34; 'to:addr :python-list': 0.35; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.39; 'data': 0.40; 'content- disposition:inline': 0.60; 'your': 0.60; 'great': 0.64; 'different': 0.64; 'cameron': 0.66; 'received:61': 0.72; '\\n\\n': 0.84; 'dict.': 0.84; 'received:110': 0.96
X-Authentication-Info Submitted using ID cskk@bigpond.com
X-Authority-Analysis v=2.0 cv=W5W6pGqk c=1 sm=1 a=LJKKp/LNQoz725nb3jBJ4Q==:17 a=yEdEr6MRgwAA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=h1PgugrvaO0A:10 a=pGLkceISAAAA:8 a=20eAzwmGAAAA:8 a=g8KEX7M-AAAA:8 a=OX15RKMA6Zx9Ty6CMmsA:9 a=CjuIK1q_8ugA:10 a=LJKKp/LNQoz725nb3jBJ4Q==:117
Date Thu, 28 May 2015 08:52:49 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: Decoding JSON file using python
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <11dbf982-efc1-4ef0-b286-e3d08ba5030e@googlegroups.com>
User-Agent Mutt/1.5.23 (2014-03-12)
References <11dbf982-efc1-4ef0-b286-e3d08ba5030e@googlegroups.com>
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.108.1432768474.5151.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432768474 news.xs4all.nl 2854 [2001:888:2000:d::a6]:37561
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91334

Show key headers only | View raw


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 | 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