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

Discussion in 'Python' started by gert, Jan 25, 2009.

  1. gert

    gert Guest

    gert, Jan 25, 2009
    #1
    1. Advertising

  2. gert

    Peter Otten Guest

    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
     
    Peter Otten, Jan 25, 2009
    #2
    1. Advertising

  3. gert

    livibetter Guest

    On Jan 26, 5:12 am, gert <> wrote:
    > raise ValueError(errmsg("Expecting property name", s, end))http://docs.python.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 = json.loads('{"test":"test"}')
    >>> v

    {u'test': u'test'}
     
    livibetter, Jan 25, 2009
    #3
  4. 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
    >
     
    J. Clifford Dyer, Jan 25, 2009
    #4
  5. > 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?
     
    Дамјан ГеоргиевÑки, Jan 25, 2009
    #5
  6. gert

    gert Guest

    On Jan 25, 11:16 pm, Дамјан ГеоргиевÑки <> 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 ?
     
    gert, Jan 25, 2009
    #6
  7. gert

    gert Guest

    On Jan 25, 11:51 pm, "Diez B. Roggisch" <> wrote:
    > gert schrieb:
    >
    > > On Jan 25, 11:16 pm, Дамјан ГеоргиевÑки <> 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.


    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.
     
    gert, Jan 25, 2009
    #7
  8. >>
    >> 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
     
    Diez B. Roggisch, Jan 25, 2009
    #8
  9. On Sun, 25 Jan 2009 23:51:41 +0100 "Diez B. Roggisch"
    <> 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).
     
    Andreas Waldenburger, Jan 25, 2009
    #9
  10. En Sun, 25 Jan 2009 21:08:04 -0200, gert <> escribió:
    > On Jan 25, 11:51 pm, "Diez B. Roggisch" <> wrote:
    >> gert schrieb:
    >>
    >> > On Jan 25, 11:16 pm, Дамјан ГеоргиевÑки <> 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
     
    Gabriel Genellina, Jan 26, 2009
    #10
  11. gert

    Steve Holden Guest

    Andreas Waldenburger wrote:
    > On Sun, 25 Jan 2009 23:51:41 +0100 "Diez B. Roggisch"
    > <> 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/
     
    Steve Holden, Jan 26, 2009
    #11
  12. gert

    gert Guest

    On Jan 26, 12:40 am, "Diez B. Roggisch" <> 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.
     
    gert, Jan 26, 2009
    #12
  13. 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"
    > > <> 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).
     
    Andreas Waldenburger, Jan 26, 2009
    #13
  14. 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"
    >> <> 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
     
    Steven D'Aprano, Jan 26, 2009
    #14
  15. gert wrote:
    > On Jan 25, 11:16 pm, Дамјан ГеоргиевÑки <> 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>
    --
     
    Matt Nordhoff, Jan 26, 2009
    #15
  16. Matt Nordhoff wrote:
    > gert wrote:
    >> On Jan 25, 11:16 pm, Дамјан ГеоргиевÑки <> 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>)
    --
     
    Matt Nordhoff, Jan 26, 2009
    #16
  17. gert

    Tim Roberts Guest

    Andreas Waldenburger <> 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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Jan 26, 2009
    #17
  18. 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
     
    Ivan Illarionov, Jan 26, 2009
    #18
  19. gert schrieb:
    > On Jan 26, 12:40 am, "Diez B. Roggisch" <> 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
     
    Diez B. Roggisch, Jan 26, 2009
    #19
  20. gert

    Miles Guest

    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
     
    Miles, Jan 26, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    245
    Florian Frank
    Jun 30, 2009
  2. Roy Smith
    Replies:
    1
    Views:
    562
    Terry Reedy
    Jun 27, 2012
  3. sajuptpm
    Replies:
    2
    Views:
    347
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    262
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    266
    Bryan Britten
    May 28, 2013
Loading...

Share This Page