json: python 2.5: error in browser

Chris Rebert clp2 at rebertia.com
Sat Sep 3 11:21:44 CEST 2011


On Sat, Sep 3, 2011 at 1:58 AM, Vineet Deodhar <d_vineet at yahoo.com> wrote:
> Opera Dragonfly shows this error:--
>
> Uncaught exception: SyntaxError: JSON.parse: Unable to parse value: A,B,C
> Error thrown at line 3, column 0 in http://127.0.0.1:8000/mywheels/test/cat:
>     var ctg = JSON.parse(["A", "B", "C"]);
> ~~~~~~~~~~~~~~~~
> I validated this json --  ["A", "B", "C"]   in jsonlint.com
> It is valid json.
>
> Why the method "JSON.parse" is unable to parse JSON?

It takes a *string* containing JSON. You want:
var ctg = JSON.parse('["A", "B", "C"]');// note outer quotes!

Think about it: If you already had ["A", "B", "C"] in the first place,
then you could just do:
var ctg = ["A", "B", "C"];
And JSON would never enter into the discussion.

Your issue is akin to confusing the following two outputs:
Python 2.6.6 (r266:84292, Jan 12 2011, 13:35:00)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'foo' # expression resulting in the string
'foo'
>>> print 'foo' # contents of the string
foo

Regards,
Chris



More information about the Python-list mailing list