I'm try to create a simple JSON object from the output of a database query that would look something like this:
json_obj= {
'1': 'test_1',
'2': 'test_2',
'3': 'test_3',
'4': 'test_4',
'5': 'test_5',
}
So far I've been trying for loops and json.dumps but cannot get it right:
cat_list = []
cats = Category.query.all()
for cat in cats:
item = {
cat.id: cat.name
}
cat_list.append(item)
json_cat_list = json.dumps(cat_list)
This does create a JSON object but not exactly what I'm looking for.
json_obj= {
{'1': 'test_1'},
{'2': 'test_2'},
{'3': 'test_3'},
{'4': 'test_4'},
{'5': 'test_5'},
}
Any suggestions on how to do this?
Many thanks