Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108436
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) |
| Date | 2016-05-09 22:17 +0200 |
| Organization | None |
| Message-ID | <mailman.549.1462825072.32212.python-list@python.org> (permalink) |
| References | <137a2593-c307-405f-8f23-3959099ff16f@googlegroups.com> <ngqr8s$jtg$1@ger.gmane.org> |
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 "<input>", line 1, in <module>
> 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'}
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) zljubisic@gmail.com - 2016-05-09 12:56 -0700
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) Peter Otten <__peter__@web.de> - 2016-05-09 22:17 +0200
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) MRAB <python@mrabarnett.plus.com> - 2016-05-09 21:21 +0100
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-05-09 21:41 +0100
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) Terry Reedy <tjreedy@udel.edu> - 2016-05-09 19:07 -0400
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) Steven D'Aprano <steve@pearwood.info> - 2016-05-10 12:13 +1000
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) Rustom Mody <rustompmody@gmail.com> - 2016-05-09 21:22 -0700
Re: json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0) zljubisic@gmail.com - 2016-05-10 12:27 -0700
csiph-web