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:
- 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?
- 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!