Google App Engine: How can I turn results from an API into a Python dictionary?
-
I'm making an app using Google app engine. I want to query an API, get JSON results, and use the dictionary in my app. Many tutorials I've seen for this use Python libraries that I don't seem to have natively in Google App Engine. Is there an easy way to do it with Python default libraries? Alternatively, how can I install necessary libraries in GAE?
-
Answer:
Google AppEngine has now been upgraded to Python 2.7, so you can use the built-in json module. import json data = '{"site": "quora", "url": "http://quora.com/", "metadata": {"visitors": 10000}}' response = json.loads(data) You can then access the fields in this dictionary as you would any other dictionary. E.g., response['site'] will output "quora", and response['metadata']['visitors'] will output 10000. Python pre-2.6 does not have the json module, so if you're using an older Python, you'll have to use simplejson, which exists in both Django and its own package (pip install simplejson or pip install django). Django 1.2 is pre-installed on the AppEngine Python environment. Simplejson has an identical API to the built-in json module. try: import json except ImportError: try: import simplejson as json except ImportError: from django.utils import simplejson as json data = '{"site": "quora", "url": "http://quora.com/"}' response = json.loads(data)
Dan Loewenherz at Quora Visit the source
Other answers
For Python3.x one will need a simple modification to the answer by Dan Loewenherz(to account for text and data instead of Unicode and 8-bit): def _jsonConvertByteToString(response): data = response.decode('utf-8') return json.loads(data) For more details see: http://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit
Shubhra Sharma (शà¥à¤à¥à¤°à¤¾ शरà¥à¤®à¤¾)
Related Q & A:
- How can I turn a trial version game into the full version?Best solution by Yahoo! Answers
- How can I turn around the image of a video?Best solution by Yahoo! Answers
- How Can i turn a black and white picture back into a colored picture?Best solution by Yahoo! Answers
- How can I turn my dual sub box into a single sub box?Best solution by Yahoo! Answers
- How can I turn my idea into a product?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.