You could convert the input you're getting to a dictionary without the need to actually construct it by yourself using json.loads or ast.literal_eval:
If you want to use json.loads you will need to fix the format, as using single quotes for the keys in JSON is invalid:
import json
json_response = "[{'address': '123 Any Street', 'province': 'ON', 'postal_code': 'L1P 2X2', 'co': 'BURL'}]"
json_response = json_response.replace('\'', '\"')
dictionary_result = json.loads(json_response)[0]
print(type(dictionary_result))
print(dictionary_result)
Which results in:
<class 'dict'>
[{'address': '123 Any Street', 'province': 'ON', 'postal_code': 'L1P 2X2', 'co': 'BURL'}]
Or use the ast.literal_eval method which takes a string and literally evaluating it as is:
import ast
json_response = "[{'address': '123 Any Street', 'province': 'ON', 'postal_code': 'L1P 2X2', 'co': 'BURL'}]"
dictionary_result = ast.literal_eval(json_response)[0]
print(type(dictionary_result))
print(dictionary_result)
Which outputs the same as the first example.
If you're getting a list, and not a string that represents one, you could just retrieve the first element:
json_response = [{'address': '123 Any Street', 'province': 'ON', 'postal_code': 'L1P 2X2', 'co': 'BURL'}]
dictionary_result = json_response[0]
json.loads()and obtain first element, no need to build dict manually.results_activity = list(qry_activity.all())jsonwon't parse it correctly as it is not a valid json (using only single quotes). I would suggestast.literal_eval