How to pass an array of integers as a parameter from javascript to python?
Question: I have a javascript code that obtains the value of several checked boxes and
inserts their value (integers) into an array:

    
    
    var my_list = $('.item:checked').map(function(){return $(this).attr('name');}).get();
    

I want to pass this javascript array to a python function as a paramter. This
is how I am doing it right now, with ajax and jQuery:

    
    
    $.ajax({
        url : "{{tg.url('/foo/bar')}}",
        data : {
                my_list: JSON.stringify(my_list),
            ...
        },
    

On the python side, I have this code, to get the parameters:

    
    
    item_list = get_paramw(kw, 'my_list', unicode)
    

This works, but I am not receiving the array as an array of integers, but as a
single string containing the "[", "]", "," and """ symbols, which I would have
to parse and isn't the most elegant way of dealing with this whole situation I
think.

How should it be done to receive a javascript array of integers as an array of
integers in python (not as a string)?


Answer: The easiest way to send simple arbitrarily-structured data from Javascript to
Python is JSON. You've already got the Javascript side of it, in that
`JSON.stringify(my_list)`. All you need is the Python side. And it's one line
of code:

    
    
    item_list_json = get_paramw(kw, 'my_list', unicode)
    item_list = json.loads(item_list_json)
    

If `item_list` in your JS code were the Javascript array of numbers `[1, 2, 3,
4]`, `item_list` in your Python code would be the Python list of ints `[1, 2,
3, 4]`.

(Well, you also have to `import json` at the top of your script, so I guess
it's two lines.)

