{ "cells": [ { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"help\": \"https://data.gov.sg/api/3/action/help_show?name=datastore_search\", \n", " \"result\": {\n", " \"_links\": {\n", " \"next\": \"/api/action/datastore_search?offset=10&limit=10&resource_id=a807b7ab-6cad-4aa6-87d0-e283a7353a0f\", \n", " \"start\": \"/api/action/datastore_search?limit=10&resource_id=a807b7ab-6cad-4aa6-87d0-e283a7353a0f\"\n", " }, \n", " \"fields\": [\n", " {\n", " \"id\": \"_id\", \n", " \"type\": \"int4\"\n", " }, \n", " {\n", " \"id\": \"quarter\", \n", " \"type\": \"text\"\n", " }, \n", " {\n", " \"id\": \"volume_of_mobile_data\", \n", " \"type\": \"numeric\"\n", " }\n", " ], \n", " \"limit\": 10, \n", " \"records\": [\n", " {\n", " \"_id\": 1, \n", " \"quarter\": \"2004-Q3\", \n", " \"volume_of_mobile_data\": \"0.000384\"\n", " }, \n", " {\n", " \"_id\": 2, \n", " \"quarter\": \"2004-Q4\", \n", " \"volume_of_mobile_data\": \"0.000543\"\n", " }, \n", " {\n", " \"_id\": 3, \n", " \"quarter\": \"2005-Q1\", \n", " \"volume_of_mobile_data\": \"0.00062\"\n", " }, \n", " {\n", " \"_id\": 4, \n", " \"quarter\": \"2005-Q2\", \n", " \"volume_of_mobile_data\": \"0.000634\"\n", " }, \n", " {\n", " \"_id\": 5, \n", " \"quarter\": \"2005-Q3\", \n", " \"volume_of_mobile_data\": \"0.000718\"\n", " }, \n", " {\n", " \"_id\": 6, \n", " \"quarter\": \"2005-Q4\", \n", " \"volume_of_mobile_data\": \"0.000801\"\n", " }, \n", " {\n", " \"_id\": 7, \n", " \"quarter\": \"2006-Q1\", \n", " \"volume_of_mobile_data\": \"0.00089\"\n", " }, \n", " {\n", " \"_id\": 8, \n", " \"quarter\": \"2006-Q2\", \n", " \"volume_of_mobile_data\": \"0.001189\"\n", " }, \n", " {\n", " \"_id\": 9, \n", " \"quarter\": \"2006-Q3\", \n", " \"volume_of_mobile_data\": \"0.001735\"\n", " }, \n", " {\n", " \"_id\": 10, \n", " \"quarter\": \"2006-Q4\", \n", " \"volume_of_mobile_data\": \"0.003323\"\n", " }\n", " ], \n", " \"resource_id\": \"a807b7ab-6cad-4aa6-87d0-e283a7353a0f\", \n", " \"total\": 47\n", " }, \n", " \"success\": true\n", "}\n" ] } ], "source": [ "import urllib\n", "import json\n", "\n", "def pp_json(json_thing, sort=True, indents=4):\n", " if type(json_thing) is str:\n", " print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents))\n", " else:\n", " print(json.dumps(json_thing, sort_keys=sort, indent=indents))\n", " return None\n", "\n", "url = 'https://data.gov.sg/api/action/datastore_search?resource_id=a807b7ab-6cad-4aa6-87d0-e283a7353a0f&limit=10'\n", "fileobj = urllib.urlopen(url)\n", "result = fileobj.read()\n", "pp_json(result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }