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