Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON string into Python dictionary

I don't have much experience in Python and I've ran into problem converting sql query data which is technically a list containing a JSON string into a Python dictionary. I'm querying the sqlite3 database which is returning a piece of data like this:

def get_answer(self, id):
    self.__cur.execute('select answer from some_table where id= %s;' % id)
    res_data = self.__cur.fetchall()
    return res_data

The data is a single JSON format element which its simplified version looks like this:

[
 {"ind": [ {"v": 101}, {"v": 102}, {"v": 65 }]},
 {"ind": [ {"v": 33}, {"v": 102}, {"v": 65}]}
]

But when I try to convert the res_data to JSON, with code like this:

temp_json = simplejson.dumps(get_answer(trace_id))

it returns a string and when I get the len(temp_json) it returns the number of characters in res_data instead of the number of objects in res_data. However, if I use Visual Studio's JSON visualizer on what get_answer(trace_id) returns, it properly shows each of the objects res_data.

I also tried to convert the res_data to a dictionary with code like this:

dict_data = ast.literal_eval(Json_data)

or

dict_data = ast.literal_eval(Json_data[0])

and in both cases it throws a "malformed string" exception. I tried to write it to a file and read it back as a JSON but it didn't worked.

Before doing that I had the copy pasted the res_data manually and used:

with open(file_name) as json_file:
    Json_data = simplejson.load(json_file)

and it worked like a charm. I've been experimenting different ways stated in SO and elsewhere but although the problem seems very straight forward, I still haven't found a solution so your help is highly appreciated.

like image 402
Ali Kahaei Avatar asked Dec 25 '22 11:12

Ali Kahaei


1 Answers

OK, I finally found the solution:

states = json.loads(temp_json[0][0])

one confusing issue was that states = json.loads(temp_json[0]) was throwing the "Expected string or buffer" exception and temp_json was a list containing only one element, so I didn't think I will get anything from temp_json[0][0].

I hope it helps others too!

like image 118
Ali Kahaei Avatar answered Dec 27 '22 10:12

Ali Kahaei