Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) Date: Mon, 09 May 2016 22:17:30 +0200 Organization: None Lines: 44 Message-ID: References: <137a2593-c307-405f-8f23-3959099ff16f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de ty1ra62e4igY7y/Tj6kd8QXAnh4lypC80vGqG9b7mRHA== 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; 'resulting': 0.04; 'error:': 0.05; 'python3': 0.05; "'',": 0.07; 'column': 0.07; 'prefix': 0.07; 'valueerror:': 0.07; '(char': 0.09; 'expected.': 0.09; 'obj,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:f 30': 0.15; '"b"': 0.16; '0)"': 0.16; 'decode': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'value",': 0.16; 'value:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'byte': 0.18; 'string,': 0.18; 'variable': 0.18; '>>>': 0.20; '"",': 0.22; 'exec': 0.22; "skip:' 40": 0.22; 'this:': 0.23; '(most': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:_ 20': 0.26; 'error': 0.27; 'skip:( 20': 0.28; 'looks': 0.29; 'loads': 0.29; "skip:' 50": 0.29; 'convert': 0.29; 'raise': 0.29; 'skip:b 40': 0.29; 'getting': 0.33; 'skip:j 20': 0.33; 'traceback': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'unicode': 0.35; 'community': 0.36; 'but': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:v 20': 0.38; 'files': 0.38; 'hi,': 0.38; 'end': 0.39; 'why': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'leading': 0.61; 'email addr:gmail.com': 0.62; 'subject:value': 0.84; 'edition': 0.86; 'quotation': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd94b1.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <137a2593-c307-405f-8f23-3959099ff16f@googlegroups.com> Xref: csiph.com comp.lang.python:108436 zljubisic@gmail.com wrote: > Hi, > > in python3 my variable looks like this: > > a = b'{"uuid":"5730e8666ffa02.34177329","error":""}' > str(a) = 'b\'{"uuid":"5730e8666ffa02.34177329","error":""}\'' > > If I execute the following command I get the error: > >>>> json.loads(str(a)) > Traceback (most recent call last): > File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition > 2016.1.2\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec > exec(exp, global_vars, local_vars) > File "", line 1, in > File "C:\Program Files\Python34\lib\json\__init__.py", line 318, in > loads > return _default_decoder.decode(s) > File "C:\Program Files\Python34\lib\json\decoder.py", line 343, in > decode > obj, end = self.raw_decode(s, idx=_w(s, 0).end()) > File "C:\Program Files\Python34\lib\json\decoder.py", line 361, in > raw_decode > raise ValueError(errmsg("Expecting value", s, err.value)) from None > ValueError: Expecting value: line 1 column 1 (char 0) > > Why I am getting this error? > If I set variable a to the '{"uuid":"5730e8666ffa02.34177329","error":""}' > everything works as expected. Look at the traceback: "line 1 column 1 (char 0)" mentioned in the error message is the leading "b". When you convert a byte string to unicode with str(bytestr) the "b" prefix and the quotation marks are part of the resulting string, but not valid JSON. Try a.decode() instead of str(a): >>> a = b'{"uuid":"5730e8666ffa02.34177329","error":""}' >>> json.loads(a.decode()) {'error': '', 'uuid': '5730e8666ffa02.34177329'}