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


Groups > comp.lang.python > #108435 > unrolled thread

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

Started byzljubisic@gmail.com
First post2016-05-09 12:56 -0700
Last post2016-05-10 12:27 -0700
Articles 8 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

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

Fromzljubisic@gmail.com
Date2016-05-09 12:56 -0700
Subjectjson.loads(...) ValueError: Expecting value: line 1 column 1 (char 0)
Message-ID<137a2593-c307-405f-8f23-3959099ff16f@googlegroups.com>
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.

[toc] | [next] | [standalone]


#108436

FromPeter Otten <__peter__@web.de>
Date2016-05-09 22:17 +0200
Message-ID<mailman.549.1462825072.32212.python-list@python.org>
In reply to#108435
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'}

[toc] | [prev] | [next] | [standalone]


#108437

FromMRAB <python@mrabarnett.plus.com>
Date2016-05-09 21:21 +0100
Message-ID<mailman.550.1462825275.32212.python-list@python.org>
In reply to#108435
On 2016-05-09 20:56, 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.
>
The b-prefix is Python-specific. It's not valid JSON syntax.

The JSON format is defined here:

http://www.json.org/

[toc] | [prev] | [next] | [standalone]


#108438

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-05-09 21:41 +0100
Message-ID<87shxrc6b7.fsf@bsb.me.uk>
In reply to#108435
zljubisic@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.

[toc] | [prev] | [next] | [standalone]


#108440

FromTerry Reedy <tjreedy@udel.edu>
Date2016-05-09 19:07 -0400
Message-ID<mailman.551.1462835277.32212.python-list@python.org>
In reply to#108435
On 5/9/2016 3:56 PM, 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.

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

[toc] | [prev] | [next] | [standalone]


#108444

FromSteven D'Aprano <steve@pearwood.info>
Date2016-05-10 12:13 +1000
Message-ID<573143b0$0$1593$c3e8da3$5496439d@news.astraweb.com>
In reply to#108440
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

[toc] | [prev] | [next] | [standalone]


#108448

FromRustom Mody <rustompmody@gmail.com>
Date2016-05-09 21:22 -0700
Message-ID<37add758-036c-4316-98a1-efb2f514dad1@googlegroups.com>
In reply to#108444
On Tuesday, May 10, 2016 at 7:43:17 AM UTC+5:30, Steven D'Aprano wrote:
> 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

Maybe the tutorial should have a small chapter on debugging (introspecting) showing how to use
- print
- type
- dir
And then iterating on these

[toc] | [prev] | [next] | [standalone]


#108485

Fromzljubisic@gmail.com
Date2016-05-10 12:27 -0700
Message-ID<a63e3aef-b17b-413f-8f36-55c65e80f0ea@googlegroups.com>
In reply to#108448
That was it.
Thanks guys for your help.
Best regards.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web