6

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?

0

2 Answers 2

15

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...

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

2 Comments

Using, the dict() function gave an error
@Nayan Not sure how that relates to the original question, but I guess you're not passing a list of pair-like items to it?
0

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.