Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

json.loads(...) ValueError: Expecting value: line 1 column 1 (char 0)

688 views
Skip to first unread message

zlju...@gmail.com

unread,
May 9, 2016, 3:56:18 PM5/9/16
to
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.

Regards.

Peter Otten

unread,
May 9, 2016, 4:18:06 PM5/9/16
to
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'}


MRAB

unread,
May 9, 2016, 4:21:30 PM5/9/16
to
The b-prefix is Python-specific. It's not valid JSON syntax.

The JSON format is defined here:

http://www.json.org/

Ben Bacarisse

unread,
May 9, 2016, 4:42:02 PM5/9/16
to
zlju...@gmail.com writes:

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

The result of str(a) is not a valid JSON string. It starts with a b but
a JSON string must start with a digit of one of -, ", {, [, t, f, n (the
letters being legal only if they are the start of true, false or null.

In fact (in Python 3), passing bytes to str() without an encoding is a
special case -- you get an informal string representation. I'm not sure
you can be sure what you get though I imagine it's designed to be the
same as Python 2 gave.

> If I set variable a to the
> '{"uuid":"5730e8666ffa02.34177329","error":""}' everything works as
> expected.

That string starts with { so it's OK. You probably what something like

json.loads(str(a, 'ascii'))

or maybe 'utf-8' or 'windows-1285' or... well you get the idea. You
need to say how the bytes should be turned into string characters.

--
Ben.

Terry Reedy

unread,
May 9, 2016, 7:08:11 PM5/9/16
to
This means that the two versions of 'a' are not the same. So what you
should have done to debug is print the second to see what you actually
passed to json.

Editorial: Programming classes should teach basic debugging better. I
have seen numerous newbie Stackoverflow questions where the person
should have started with adding a print statement before posting a question.

--
Terry Jan Reedy

Steven D'Aprano

unread,
May 9, 2016, 10:13:17 PM5/9/16
to
On Tue, 10 May 2016 09:07 am, Terry Reedy wrote:

> Editorial: Programming classes should teach basic debugging better. I
> have seen numerous newbie Stackoverflow questions where the person
> should have started with adding a print statement before posting a
> question.

+10000



--
Steven

Rustom Mody

unread,
May 10, 2016, 12:22:33 AM5/10/16
to
Maybe the tutorial should have a small chapter on debugging (introspecting) showing how to use
- print
- type
- dir
And then iterating on these

zlju...@gmail.com

unread,
May 10, 2016, 3:28:04 PM5/10/16
to
That was it.
Thanks guys for your help.
Best regards.
0 new messages