Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'modify': 0.04; 'string.': 0.04; 'skip:\\ 20': 0.05; 'debugging.': 0.07; 'json': 0.07; 'modifying': 0.07; 'skip:\\ 10': 0.07; 'subject:file': 0.07; 'exception,': 0.09; 'skip:\\ 30': 0.09; 'subject:using': 0.09; 'exception': 0.13; 'subject:python': 0.14; '>on': 0.16; '>they': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'hint:': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'quoted': 0.16; 'reproducing': 0.16; 'said.': 0.16; 'simpson': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'input': 0.18; 'changes': 0.20; 'first,': 0.20; 'fix': 0.21; 'meant': 0.22; 'parsing': 0.22; 'module': 0.23; 'errors': 0.23; "i've": 0.24; 'tried': 0.24; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'thus': 0.24; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'raw': 0.27; 'follows': 0.29; 'correct': 0.29; 'work.': 0.30; 'print': 0.31; 'trouble': 0.31; 'code': 0.31; 'to:addr :python-list': 0.35; 'really': 0.35; 'but': 0.36; "didn't": 0.37; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'does': 0.39; 'to:addr:python.org': 0.39; 'seem': 0.39; 'data': 0.40; 'content- disposition:inline': 0.60; 'your': 0.60; 'per': 0.61; 'different': 0.64; 'cameron': 0.66; 'below.': 0.66; 'received:61': 0.72; '>you': 0.84; 'actually,': 0.84; 'etc,': 0.84; 'suggestion,': 0.84 X-Authentication-Info: Submitted using ID cskk@bigpond.com X-Authority-Analysis: v=2.0 cv=D6DF24tj c=1 sm=1 a=wlK6PSMovxJVZ0xpcBuegA==:17 a=yEdEr6MRgwAA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=h1PgugrvaO0A:10 a=Lpnxw2taAAAA:8 a=pGLkceISAAAA:8 a=20eAzwmGAAAA:8 a=oqAdNJufS8G0U2SllsMA:9 a=CjuIK1q_8ugA:10 a=M2ogJ6dkwzhOXYXO:21 a=9o4E4LMzzvof3etq:21 a=wlK6PSMovxJVZ0xpcBuegA==:117 Date: Thu, 28 May 2015 13:32:39 +1000 From: Cameron Simpson 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: User-Agent: Mutt/1.5.23 (2014-03-12) References: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432785446 news.xs4all.nl 2900 [2001:888:2000:d::a6]:54827 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91348 On 28May2015 01:38, Jon Ribbens wrote: >On 2015-05-27, Karthik Sharma 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 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']))