I need to pass an object that I can convert using $.parseJSON
. The query looks like this:
cursor.execute("SELECT earnings, date FROM table")
What do I need to do from here in order to pass an HttpResponse object that can be converted into json?
I need to pass an object that I can convert using $.parseJSON
. The query looks like this:
cursor.execute("SELECT earnings, date FROM table")
What do I need to do from here in order to pass an HttpResponse object that can be converted into json?
Well, if you simply do:
json_string = json.dumps(cursor.fetchall())
you'll get an array of arrays...
[["earning1", "date1"], ["earning2", "date2"], ...]
Another way would be to use:
json_string = json.dumps(dict(cursor.fetchall()))
That will give you a json object with earnings
as indexes...
{"earning1": "date1", "earning2": "date2", ...}
If that's not what you want, then you need to specify how you want your result to look...
Have you investigated the json library?
The following will serialize your data base output into json. I'm not sure what you data looks like, or what you need the json to look like, but if you just keep in mind that python lists -> arrays, and python dicts -> objects, I think you'll be alright.
import json
json.dumps(data)