#!C:\Program Files\Python27\python.exe # -*- coding: UTF-8 -*- import os, sys, json, cgitb # set path to lib and import sys.path.insert(0, os.path.abspath("../rw_common")) from lib import getRecordSet #cgitb.enable() # Get list from DB rs = getRecordSet("""SELECT collection.name, access_point FROM registry.datasource JOIN registry.collection ON datasource_fk = datasource_id WHERE (protocol_fk='biocase' and datasource.active_flag) ORDER BY access_point""") # Loop through and store in temp dict l = {} for r in rs: title, url = r[0], r[1] if url in l: l[url] += [title] else: l[url] = [title] # Construct response doc and return it resp = [{'service_url': key, 'datasets': [{'title': d} for d in val] } for key, val in l.iteritems()] print "Content-Type: application/json\n" print json.dumps(resp)