I have the following structure as a list (originally returned as a result set from django ORM):
{'valuex': 65.92, 'day': datetime.date(2014, 10, 15), 'valuey': 74}{'valuex': 58.55, 'day': datetime.date(2014, 10, 16), 'valuey': 61}
And I'd like to transform it (and output it) as the following json:
{
'2014-10-15' : { 'valuex': 65.92, 'day': datetime.date(2014, 10, 15), 'valuey': 74 },
'2014-10-16' : { 'valuex': 58.55, 'day': datetime.date(2014, 10, 16), 'valuey': 61 }
}
I assumed that it would be something trivial like:
ret = {}
for r in results:
ret[r.get('day')] = r
and then outputing to json could be done by Django (@render_to_json) But that really only returns the dates with no corresponding dictionary.
I'm new to Python and I'm lost here.
UPDATE:
I found out that the actual issue is that Django is not outputing my structure as json. the script above actually does create the structure I'm trying to achieve. but django fails to convert it to json. is it safe to do:
return HttpResponse(str(ret))
instead? Or is there some way to make it render to proper json (json.dumps has the same issue).
UPDATE2: Okay, json.dumps(ret) acutally did work for me, only needed to get rid of the datetime object as it's not json seralizeable.