Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: convert python List to javascript array

2,103 views
Skip to first unread message

Benjamin Kaplan

unread,
Sep 2, 2011, 11:50:12 AM9/2/11
to pytho...@python.org
On Fri, Sep 2, 2011 at 11:34 AM, Vineet Deodhar <d_vi...@yahoo.com> wrote:
> Hi !
> Within a web framework, I want want to pass a python sequence (list or
> tuple) to client-side javascript function as an array (javascript
> compatible)
> e.g., I have this list:
> L = ['spam', 'ham', 'eggs', 12, (13.63)]
> What is the correct way to convert L to javascript array format?
> 1) jsonify the list and pass it to javascript
> (whether json format & javascript array are similar?)
>
> OR
> 2)
>>> import array
>>> y = array.array(i, L)
>  then return y to javascript function
> But the problem with this method is, it will give an array of basic values
> only.
>
> OR
> 3) Any other alternative?
>
> Thanks,
> Vineet
> --

The JavaScript Object Notation (JSON) is meant exactly for this purpose.

Chris Rebert

unread,
Sep 2, 2011, 1:26:15 PM9/2/11
to Vineet Deodhar, pytho...@python.org
On Fri, Sep 2, 2011 at 8:34 AM, Vineet Deodhar <d_vi...@yahoo.com> wrote:
> Hi !
> Within a web framework, I want want to pass a python sequence (list or
> tuple) to client-side javascript function as an array (javascript
> compatible)
> e.g., I have this list:
> L = ['spam', 'ham', 'eggs', 12, (13.63)]
> What is the correct way to convert L to javascript array format?
> 1) jsonify the list and pass it to javascript
> (whether json format & javascript array are similar?)

JSON is in fact a subset of JavaScript, and modern browsers now
include a specific API for parsing and generating it
(https://developer.mozilla.org/En/Using_native_JSON ).
Python likewise has a JSON module in the std lib:
http://docs.python.org/library/json.html

> OR
> 2)
>>> import array
>>> y = array.array(i, L)
>  then return y to javascript function
> But the problem with this method is, it will give an array of basic values
> only.

The word "array" gets tossed around a lot by programmers. The `array`
module is not at all what you want in this case.

Cheers,
Chris
--
http://rebertia.com

Chris Rebert

unread,
Sep 3, 2011, 1:50:36 AM9/3/11
to Vineet Deodhar, pytho...@python.org
On Fri, Sep 2, 2011 at 10:36 PM, Vineet Deodhar <d_vi...@yahoo.com> wrote:
>> e.g., I have this list:
>> L = ['spam', 'ham', 'eggs', 12, (13.63)]
>> What is the correct way to convert L to javascript array format?
<snip>

> Python likewise has a JSON module in the std lib:
> http://docs.python.org/library/json.html
> ========
> I tried with json in std lib.
> But it is included in python 2.6 onwards.
> For my current proj, I need to use python 2.5.
> Hence can't use json from the std lib.
> Any idea on converting list to json in python 2.5?

http://pypi.python.org/pypi/simplejson/

Cheers,
Chris

Chris Rebert

unread,
Sep 3, 2011, 5:21:44 AM9/3/11
to Vineet Deodhar, pytho...@python.org
On Sat, Sep 3, 2011 at 1:58 AM, Vineet Deodhar <d_vi...@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

0 new messages