1

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')

How do I access it form client side? Can I access it like the following?

jsonValidateReturn[0]

Or how do 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 that returns an array (see Stack Overflow question Creating a JSON response using Django and Python that handles client side POST requests, so I wanted the same thing in return with Python, but it didn't go well.

2
  • 1
    what are you using to process the response? javascript? python? Commented Mar 12, 2010 at 19:31
  • Can you update the question to provide more details. Do you have a Python server? And a Python client? How is the Python client getting in contact with the server? Does the client use urrlib2? Is that what you're doing? Commented Mar 12, 2010 at 19:53

2 Answers 2

1

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

That is, in order to give it a name, in your JavaScript code 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 code 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!

Sign up to request clarification or add additional context in comments.

1 Comment

is there any way to assign a name from the Python side
0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.