f



v = json.loads("{'test':'test'}")

raise ValueError(errmsg("Expecting property name", s, end))
http://docs.python.org/library/json.html
What am I doing wrong ?
0
gert.cuykens (246)
1/25/2009 9:12:30 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

22 Replies
1247 Views

Similar Articles

[PageSpeed] 5

gert wrote:

> raise ValueError(errmsg("Expecting property name", s, end))
> http://docs.python.org/library/json.html
> What am I doing wrong ?

You need proper quotation marks:

>>> s = json.dumps({'test':'test'})
>>> s
'{"test": "test"}'
>>> json.loads(s)
{u'test': u'test'}

The JSON format is described here: http://www.json.org/

Peter
0
__peter__ (4031)
1/25/2009 9:38:33 PM
On Jan 26, 5:12=A0am, gert <gert.cuyk...@gmail.com> wrote:
> raise ValueError(errmsg("Expecting property name", s, end))http://docs.py=
thon.org/library/json.html
> What am I doing wrong ?

You use wrong quotes, it should be wrapped by double quotes not single
quotes. Read http://json.org/:

  "A string is a collection of zero or more Unicode characters,
wrapped in double quotes, ..."

>>> v =3D json.loads('{"test":"test"}')
>>> v
{u'test': u'test'}
0
1/25/2009 9:42:11 PM
Please include all relevant information in the *body* of your message,
not just in the subject.  It's a pain having to piece a question back
together between the subject.

On Sun, 2009-01-25 at 13:12 -0800, gert wrote:
> raise ValueError(errmsg("Expecting property name", s, end))
> http://docs.python.org/library/json.html
> What am I doing wrong ?

JSON requires strings to be enclosed in double quotes.  It is not as
flexible as python when it comes to quotation.  If you change your
example to

v = json.loads('{"test":"test"}')

it will work.  (Note JSON also doesn't allow trailing commas, so
'{"test":"test",}' will also fail)

Cheers,
Cliff


> --
> http://mail.python.org/mailman/listinfo/python-list
> 

0
jcd1 (296)
1/25/2009 9:47:20 PM
> raise ValueError(errmsg("Expecting property name", s, end))
> http://docs.python.org/library/json.html
> What am I doing wrong ?
try this
v = json.loads('{"test":"test"}')

JSON doesn't support single quotes, only double quotes.


-- 
дамјан ( http://softver.org.mk/damjan/ )

A: Because it reverses the logical flow of converstion.
Q: Why is top posting frowned upon?

0
gdamjan (281)
1/25/2009 10:16:15 PM
On Jan 25, 11:16=C2=A0pm, =D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD =D0=93=D0=B5=
=D0=BE=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8 <gdam...@gmail.com> =
wrote:
> > raise ValueError(errmsg("Expecting property name", s, end))
> >http://docs.python.org/library/json.html
> > What am I doing wrong ?
>
> try this
> v =3D json.loads('{"test":"test"}')
>
> JSON doesn't support single quotes, only double quotes.

the funny part is when you print(v) you get
{'test': 'test'}

Single quotes works in every browser that support json so i
recommended python should support it too, besides it looks much
cleaner
{'test': 'test'}
{"test": "test"}

It can not be that hard to support both notation can it ?
0
gert.cuykens (246)
1/25/2009 10:28:48 PM
gert schrieb:
> On Jan 25, 11:16 pm, Дамјан Георгиевски <gdam...@gmail.com> wrote:
>>> raise ValueError(errmsg("Expecting property name", s, end))
>>> http://docs.python.org/library/json.html
>>> What am I doing wrong ?
>> try this
>> v = json.loads('{"test":"test"}')
>>
>> JSON doesn't support single quotes, only double quotes.
> 
> the funny part is when you print(v) you get
> {'test': 'test'}

So what? That's python deciding to print strings using single-quotes. 
That has nothing to do with JSON.

The important part is this:

 >>> json.dumps(json.loads('{"test":"test"}'))
'{"test": "test"}'

> Single quotes works in every browser that support json so i
> recommended python should support it too, besides it looks much
> cleaner
> {'test': 'test'}
> {"test": "test"}
> 
> It can not be that hard to support both notation can it ?

It's not hard, but it's not standard-conform.

Most browsers even accept something like this:

{foo : "bar"}

But all of this is not JSON.

Diez
0
deets (3351)
1/25/2009 10:51:41 PM
On Jan 25, 11:51=C2=A0pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> gert schrieb:
>
> > On Jan 25, 11:16 pm, =D0=94=D0=B0=D0=BC=D1=98=D0=B0=D0=BD =D0=93=D0=B5=
=D0=BE=D1=80=D0=B3=D0=B8=D0=B5=D0=B2=D1=81=D0=BA=D0=B8 <gdam...@gmail.com> =
wrote:
> >>> raise ValueError(errmsg("Expecting property name", s, end))
> >>>http://docs.python.org/library/json.html
> >>> What am I doing wrong ?
> >> try this
> >> v =3D json.loads('{"test":"test"}')
>
> >> JSON doesn't support single quotes, only double quotes.
>
> > the funny part is when you print(v) you get
> > {'test': 'test'}
>
> So what? That's python deciding to print strings using single-quotes.
> That has nothing to do with JSON.
>
> The important part is this:
>
> =C2=A0>>> json.dumps(json.loads('{"test":"test"}'))
> '{"test": "test"}'
>
> > Single quotes works in every browser that support json so i
> > recommended python should support it too, besides it looks much
> > cleaner
> > {'test': 'test'}
> > {"test": "test"}
>
> > It can not be that hard to support both notation can it ?
>
> It's not hard, but it's not standard-conform.
>
> Most browsers even accept something like this:
>
> {foo : "bar"}
>
> But all of this is not JSON.

Yes it is, you just make it more python dictionary compatible :)
What is this json person email address so I can ask that he makes a
very small update on his site.

Besides if you can make lightweight versions of standards
http://docs.python.org/library/xml.dom.minidom.html

You can defenatly add lightweight quotes to json.

0
gert.cuykens (246)
1/25/2009 11:08:04 PM
>>
>> But all of this is not JSON.
> 
> Yes it is, you just make it more python dictionary compatible :)

No, what you do is to make it more incompatible with other 
json-implementations. Which defies the meaning of a standard.

Besides, {foo : "bar"} is *not* python dictionary compatible, at least 
not unless you defined foo beforehand, and then there is no guarantee 
that foo is actually as string containing 'foo'.

> What is this json person email address so I can ask that he makes a
> very small update on his site.

Go try your luck - http://www.json.org/
> 
> Besides if you can make lightweight versions of standards
> http://docs.python.org/library/xml.dom.minidom.html

minidom is a lightweight version of the DOM-API. But it reads and writes 
standard-conform XML documents.

The same applies for element-tree and lxml.

So it does not serve as a counter-example.

> You can defenatly add lightweight quotes to json.

If you bring all other implementors of all other languages to 
simultaneously do so - yes, you can. Again, good luck with that.


Diez

0
deets (3351)
1/25/2009 11:40:09 PM
On Sun, 25 Jan 2009 23:51:41 +0100 "Diez B. Roggisch"
<deets@nospam.web.de> wrote:

> gert schrieb:
> > {'test': 'test'}
> > {"test": "test"}
> > 
> > It can not be that hard to support both notation can it ?
> 
> It's not hard, but it's not standard-conform.
> 
OK, playing the devil's advocate here: Doesn't practicality beat purity?

/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
0
geekmail (58)
1/25/2009 11:54:31 PM
En Sun, 25 Jan 2009 21:08:04 -0200, gert <gert.cuykens@gmail.com> escribió:
> On Jan 25, 11:51 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>> gert schrieb:
>>
>> > On Jan 25, 11:16 pm, Дамјан Георгиевски <gdam...@gmail.com> wrote:
>> >>> raise ValueError(errmsg("Expecting property name", s, end))
>> >>>http://docs.python.org/library/json.html
>> >>> What am I doing wrong ?
>> >> try this
>> >> v = json.loads('{"test":"test"}')
>>
>> >> JSON doesn't support single quotes, only double quotes.
>> > It can not be that hard to support both notation can it ?
>> It's not hard, but it's not standard-conform.
>>
>> Most browsers even accept something like this:
>>
>> {foo : "bar"}
>>
>> But all of this is not JSON.
>
> Yes it is, you just make it more python dictionary compatible :)

What do you mean? The above is not valid Python.
JSON is whatever the author says it is. And he says "A string is a  
collection of zero or more Unicode characters, wrapped in double quotes,  
using backslash escapes".

> What is this json person email address so I can ask that he makes a
> very small update on his site.

Try http://www.json.org/ -- good luck.

> Besides if you can make lightweight versions of standards
> http://docs.python.org/library/xml.dom.minidom.html

This is not a lightweight version of XML, but a lightweight version of an  
API. minidom reads and writes the same valid XML documents.

> You can defenatly add lightweight quotes to json.

JSON is ligthweight *already*: "JSON (JavaScript Object Notation) is a  
lightweight data-interchange format."
Introducing single quoted strings, apart from being incompatible with the  
previous version, would make parsing more complex.

-- 
Gabriel Genellina

0
gagsl-py2 (3707)
1/26/2009 12:04:40 AM
Andreas Waldenburger wrote:
> On Sun, 25 Jan 2009 23:51:41 +0100 "Diez B. Roggisch"
> <deets@nospam.web.de> wrote:
> 
>> gert schrieb:
>>> {'test': 'test'}
>>> {"test": "test"}
>>>
>>> It can not be that hard to support both notation can it ?
>> It's not hard, but it's not standard-conform.
>>
> OK, playing the devil's advocate here: Doesn't practicality beat purity?
> 
It's not practical to expect a standard to be rewritten to conform with
the ideas of one individual, as well as all the implementations of that
standard.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/

0
steve73 (4801)
1/26/2009 12:04:44 AM
On Jan 26, 12:40=A0am, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> >> But all of this is not JSON.
>
> > Yes it is, you just make it more python dictionary compatible :)
>
> No, what you do is to make it more incompatible with other
> json-implementations. Which defies the meaning of a standard.
>
> Besides, {foo : "bar"} is *not* python dictionary compatible, at least
> not unless you defined foo beforehand, and then there is no guarantee
> that foo is actually as string containing 'foo'.
>
> > What is this json person email address so I can ask that he makes a
> > very small update on his site.
>
> Go try your luck -http://www.json.org/
>
> > Besides if you can make lightweight versions of standards
> >http://docs.python.org/library/xml.dom.minidom.html
>
> minidom is a lightweight version of the DOM-API. But it reads and writes
> standard-conform XML documents.
>
> The same applies for element-tree and lxml.
>
> So it does not serve as a counter-example.

yes it does because adding ' does not mean replacing " so it will
still load standard json. Like every browser does and is exactly the
same philosofie as

http://docs.python.org/library/xml.dom.minidom.html

The xml.dom.minidom module is essentially a DOM 1.0-compatible DOM
with some DOM 2 features (primarily namespace features).

or

unlink() is a xml.dom.minidom-specific extension to the DOM API. After
calling unlink() on a node, the node and its descendants are
essentially useless.







0
gert.cuykens (246)
1/26/2009 12:05:59 AM
On Sun, 25 Jan 2009 19:04:44 -0500 Steve Holden <steve@holdenweb.com>
wrote:

> Andreas Waldenburger wrote:
> > On Sun, 25 Jan 2009 23:51:41 +0100 "Diez B. Roggisch"
> > <deets@nospam.web.de> wrote:
> > 
> >> gert schrieb:
> >>> {'test': 'test'}
> >>> {"test": "test"}
> >>>
> >>> It can not be that hard to support both notation can it ?
> >> It's not hard, but it's not standard-conform.
> >>
> > OK, playing the devil's advocate here: Doesn't practicality beat
> > purity?
> > 
> It's not practical to expect a standard to be rewritten to conform
> with the ideas of one individual, as well as all the implementations
> of that standard.
> 
But as gert says, the standard is "broken" by many many browsers
already (I don't know if that's true, though; I just assume it is). Why
not make it compatible with, and as forgiving as, those?

(I feel a bit stupid here, because I'm basically on the "adhere to the
standard" side. I just noticed that the Zen (or what I make of it)
seems to suggest otherwise.)

regards,
/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
0
geekmail (58)
1/26/2009 12:57:44 AM
On Sun, 25 Jan 2009 19:04:44 -0500, Steve Holden wrote:

> Andreas Waldenburger wrote:
>> On Sun, 25 Jan 2009 23:51:41 +0100 "Diez B. Roggisch"
>> <deets@nospam.web.de> wrote:
>> 
>>> gert schrieb:
>>>> {'test': 'test'}
>>>> {"test": "test"}
>>>>
>>>> It can not be that hard to support both notation can it ?
>>> It's not hard, but it's not standard-conform.
>>>
>> OK, playing the devil's advocate here: Doesn't practicality beat
>> purity?
>> 
> It's not practical to expect a standard to be rewritten to conform with
> the ideas of one individual, as well as all the implementations of that
> standard.


Supposedly "every browser" (what, all of them?) already support a de 
facto extension to the JSON standard, allowing more flexible quoting. In 
an ideal world, yes the standard should change to conform to what "all 
browsers" do, since what they do is sensible. In my opinion, Python's 
handling of quotes simply is The Right Way To Do It.

But more realistically, I think a more attainable solution will be for 
json.loads() to give a better error message than "Expecting property 
name" when you use the wrong quotes.


-- 
Steven
0
steve9679 (1985)
1/26/2009 1:07:12 AM
gert wrote:
> On Jan 25, 11:16 pm, Дамјан Георгиевски <gdam...@gmail.com> wrote:
>>> raise ValueError(errmsg("Expecting property name", s, end))
>>> http://docs.python.org/library/json.html
>>> What am I doing wrong ?
>> try this
>> v = json.loads('{"test":"test"}')
>>
>> JSON doesn't support single quotes, only double quotes.
> 
> the funny part is when you print(v) you get
> {'test': 'test'}
> 
> Single quotes works in every browser that support json so i
> recommended python should support it too, besides it looks much
> cleaner
> {'test': 'test'}
> {"test": "test"}
> 
> It can not be that hard to support both notation can it ?

There's a difference between JavaScript source code and JSON. AFAICT
from the source [1], Mozilla's JSON parser doesn't accept single quotes.

[1] <http://mxr.mozilla.org/mozilla-central/source/js/src/json.cpp>
-- 
0
mnordhoff (148)
1/26/2009 2:17:58 AM
Matt Nordhoff wrote:
> gert wrote:
>> On Jan 25, 11:16 pm, Дамјан Георгиевски <gdam...@gmail.com> wrote:
>>>> raise ValueError(errmsg("Expecting property name", s, end))
>>>> http://docs.python.org/library/json.html
>>>> What am I doing wrong ?
>>> try this
>>> v = json.loads('{"test":"test"}')
>>>
>>> JSON doesn't support single quotes, only double quotes.
>> the funny part is when you print(v) you get
>> {'test': 'test'}
>>
>> Single quotes works in every browser that support json so i
>> recommended python should support it too, besides it looks much
>> cleaner
>> {'test': 'test'}
>> {"test": "test"}
>>
>> It can not be that hard to support both notation can it ?
> 
> There's a difference between JavaScript source code and JSON. AFAICT
> from the source [1], Mozilla's JSON parser doesn't accept single quotes.
> 
> [1] <http://mxr.mozilla.org/mozilla-central/source/js/src/json.cpp>

By the way, I forgot to add, according to the ECMA-262 standard (page
18, section 7.8.4) [1], ECMAScript string literals can use either double
or single quotes, so that's not a browser-specific extension.

[1]
<http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf>
(<http://xrl.us/bedr3c>)
-- 
0
mnordhoff (148)
1/26/2009 2:38:48 AM
Andreas Waldenburger <geekmail@usenot.de> wrote:
> 
>But as gert says, the standard is "broken" by many many browsers
>already 

We're debating relatively picky semantic point, so I won't feel bad by
being picky.

Browsers have nothing to do with the JSON standard.  JSON is not
Javascript, nor is it a part of Javascript.  JSON is a data exchange
standard, which happens to be readable to Javascript parsers.  A valid JSON
expression happens to be a valid Javascript expression, but not vice versa.

That's just the way it is.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
0
timr (1409)
1/26/2009 5:00:37 AM
Diez wrote:
> gert schrieb:
> > Single quotes works in every browser that support json so i
> > recommended python should support it too, besides it looks much
> > cleaner
> > {'test': 'test'}
> > {"test": "test"}
>
> > It can not be that hard to support both notation can it ?
>
> It's not hard, but it's not standard-conform.
>
> Most browsers even accept something like this:
>
> {foo : "bar"}
>
> But all of this is not JSON.

By the way, all of this *is* YAML:

>>> yaml.load("{'test':'test'}")
{'test': 'test'}
>>> yaml.load("{test: test}")
{'test': 'test'}

If someone needs more forgiving and user-editable format,
YAML might be a good choice.

--
Ivan
0
1/26/2009 8:18:17 AM
gert schrieb:
> On Jan 26, 12:40 am, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>>>> But all of this is not JSON.
>>> Yes it is, you just make it more python dictionary compatible :)
>> No, what you do is to make it more incompatible with other
>> json-implementations. Which defies the meaning of a standard.
>>
>> Besides, {foo : "bar"} is *not* python dictionary compatible, at least
>> not unless you defined foo beforehand, and then there is no guarantee
>> that foo is actually as string containing 'foo'.
>>
>>> What is this json person email address so I can ask that he makes a
>>> very small update on his site.
>> Go try your luck -http://www.json.org/
>>
>>> Besides if you can make lightweight versions of standards
>>> http://docs.python.org/library/xml.dom.minidom.html
>> minidom is a lightweight version of the DOM-API. But it reads and writes
>> standard-conform XML documents.
>>
>> The same applies for element-tree and lxml.
>>
>> So it does not serve as a counter-example.
> 
> yes it does because adding ' does not mean replacing " so it will
> still load standard json. Like every browser does and is exactly the
> same philosofie as

No. If minidom would accept XML-documents that contain newlines in 
attributes (which otherwise are forbidden), e.g.

<foo bar="some
text"/>

*that* would be like adding single-quote stringliterals to JSON.

This is about the *format*, not the API.

There are people who say something along the lines of "be strict when 
writing, and tolerant when reading" (the exact quote is different, but 
neither google:~site:mybrain nor any other have helped me here), so one 
could argue that reading JSON that is not standard-conform would be ok.

But IMHO that would increase the amount of interoperability-problems - 
because some people would *write* json that isn't standard-conform anymore.

Diez

0
deets (3351)
1/26/2009 9:06:27 AM
On Mon, Jan 26, 2009 at 4:06 AM, Diez B. Roggisch wrote:
> There are people who say something along the lines of "be strict when
> writing, and tolerant when reading" (the exact quote is different, but
> neither google:~site:mybrain nor any other have helped me here)

That's Postel's Law:
http://en.wikipedia.org/wiki/Robustness_Principle

-Miles
0
1/26/2009 7:06:44 PM
On Sun, 2009-01-25 at 14:28 -0800, gert wrote:
> On Jan 25, 11:16 pm, Дамјан Георгиевски <gdam...@gmail.com> wrote:
> > > raise ValueError(errmsg("Expecting property name", s, end))
> > >http://docs.python.org/library/json.html
> > > What am I doing wrong ?
> >
> > try this
> > v = json.loads('{"test":"test"}')
> >
> > JSON doesn't support single quotes, only double quotes.
> 
> the funny part is when you print(v) you get
> {'test': 'test'}
> 
> Single quotes works in every browser that support json so i
> recommended python should support it too, besides it looks much
> cleaner
> {'test': 'test'}
> {"test": "test"}
> 
> It can not be that hard to support both notation can it ?

It's not that hard, but it does add a noticeable level of complexity to
the process.  In JSON, you simply tell the parser, "if you see a
double-quote character, we're now parsing a string.  When you see
another (unescaped) double-quote character, the string is done.

The equivalent version for your extended JSON would say, "if you see a
quote character of either kind, we're parsing a string.  When you see
another (unescaped) quote character of either kind, the string is done."
But that doesn't work, because it would allow {'test": "test'}.  So the
parser has to remember which quote character is being used, and require
those characters to be escaped, and not the other (but then does "te
\'st" render as r"te'st" or r"te\'st"?) and only close the string when
the appropriate quote is found.  

Not an impossible task, but certainly more complex than the current
parsing requirements for JSON.

Cheers,
Cliff


0
jcd1 (296)
1/27/2009 4:37:35 PM
"Steven D'Aprano" <steve@REMOVE-THIS-cybersource.com.au> wrote in message 
news:018d0300$0$20629$c3e8da3@news.astraweb.com...

> Supposedly "every browser" (what, all of them?) already support a de
> facto extension to the JSON standard, allowing more flexible quoting.

That's a consequence of JSON being a subset of Javascript syntax,
so you can just call eval() on it, if you're willing. When you use a
library, it's pot luck whether it accepts JSON-soup or not. 


0
R.Brodie (551)
1/27/2009 5:11:59 PM
Reply: