Working Ninja
2019-01-26T09:36:57

Objective: Sort dictionary by nested dictionary value

Let's say we want to sort the following JSON by the nested dictionary value of count.

{ "Mail": { "count": 20, "users": ["lukeskywalker", "darthvader"] }, "Droid Sync": { "count": 5, "users": ["lukeskywalker"] } }

Method 1: lambda

READ MORE

2016-11-11T16:55:21
import json polls = Poll.objects.all() # Convert our QuerySet into a list so polls can be serialized print json.dumps(list(p), indent=2) JsonResponse({'polls': list(polls)})

Source: http://stackoverflow.com/questions/7650448/django-serialize-queryset-values-into-json

READ MORE

2016-11-03T20:05:28
import json import psycopg2 from psycopg2.extras import RealDictCursor cursor = conn.cursor(cursor_factory=RealDictCursor) objects = cursor.fetchall() print json.dumps(objects, indent=2) # Django #JsonResponse({'objects': objects})

Source: https://www.peterbe.com/plog/from-postgres-to-json-strings

READ MORE