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

J

J. Clifford Dyer

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.

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

 
G

gert

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

gert

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"}'



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.
 
D

Diez B. Roggisch

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
 
G

Gabriel Genellina

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.
 
S

Steve Holden

Andreas said:
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
 
G

gert

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'.


Go try your luck -http://www.json.org/


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.
 
A

Andreas Waldenburger

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
 
S

Steven D'Aprano

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.
 
M

Matt Nordhoff

gert said:
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>
--
 
M

Matt Nordhoff

Matt said:
gert said:
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>)
--
 
T

Tim Roberts

Andreas Waldenburger said:
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.
 
I

Ivan Illarionov

Diez said:
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:
{'test': 'test'}

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

Diez B. Roggisch

gert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top