views:

301

answers:

2

Hi,

I'm trying to send a Python list in to client side (encoded as JSON), this is the code snippet which I have written:

array_to_js = [vld_id, vld_error, False]
array_to_js[2] = True
jsonValidateReturn = simplejson.dumps(array_to_js)
return HttpResponse(jsonValidateReturn, mimetype='application/json')

So my question is how to access it form client side, can I access it like this:

jsonValidateReturn[0]

or how I assign a name to the returned JSON array in order to access it?

Actually I'm trying to convert a server side AJAX script(returns an array) that handles client side POST requests, so I wanted the same thing in return with Python but it didn't go well.

A: 

The json array will be dumped without a name / assignment.

I.E. in order to give it a name, in your javascript you would do something like this:

var my_json_data_dump = function_that_gets_json_data();

If you want to visualize it, for example, substitute:

var my_json_data_dump = { 'first_name' : Bob, 'last_name': smith };

Also, like Iganacio said, you're going to need something like json2.js to parse the string into the object in the last example. You could wrap that parsing step inside of function_that_gets_json_data, or if you're using jQuery you can do it with a function like jQuery.getJSON().

json2.js is still nice to have, though.


In response to the comment ( I need space and markup)

Yes of course. All the python side is doing is encoding a string representation (json) for you. You could do something like 'var blah = %s' % json.dumps(obj_to_encode) and then on the client side, instead of simply parsing the response as json, you parse it as javascript.

I wouldn't recommend this for a few reasons:

  1. You're no longer outputting json. What if you want to use it in a context where you don't want the variable name, or can't parse javascript?
  2. You're evaluating javascript instead of simply parsing json. It's an operation that's open to security holes (if someone can seed the data, they might be able to execute a XSS attack).

I guess you're facing something I think every ajax developer runs in to. You want one place of truth in your application, but now you're being encouraged to define variables and whatnot in javascript. So you have to cross reference your python code with the javascript that uses it.

I wouldn't get too hung up on it. I can't see why you would absolutely need to control the name of the variable from python in this manner. If you're counting on the variable name being the same so that you can reference it in subsequent javascript or python code, it's something you might obviate by simply restructuring your code. I don't mean that as a criticism, just a really helpful (in general) suggestion!

Koobz
is there any way to assign a name from the Python side
MMRUser
A: 

If both client and server are in Python, here's what you need to know.

Server. Use a dictionary to get labels on the fields. Write this as the response.

>>> import json
>>> json.dumps( {'vld_id':1,'vls_error':2,'something_else':True} )
'{"vld_id": 1, "something_else": true, "vls_error": 2}'

Client. After reading the response string, create a Python dictionary this way.

>>> json.loads( '{"vld_id": 1, "something_else": true, "vls_error": 2}' )
{u'vld_id': 1, u'something_else': True, u'vls_error': 2}
S.Lott