Python 3 - Parse JSON of several API requests in a list and output to a file

advertisements

How do I...

1) Parse JSON objects from API queries in Python 3

2) Parse multiple requests into a list, and

3) Output the list into a JSON file


I prefer using requests for all API programming. Here is a one-liner that fetches the results of several API calls, puts them in a list, and writes that list to a JSON file:

json.dump([requests.get(url).json() for url in URLs], fp)

Here is a complete test program:

import requests
import json

URLs = [
    # Some URLs that return JSON objects
    'http://httpbin.org/ip',
    'http://httpbin.org/user-agent',
    'http://httpbin.org/headers'
]

with open('result.json', 'w') as fp:
    json.dump([requests.get(url).json() for url in URLs], fp, indent=2)

If you are allergic to requests for some reason, here is equivalent Python3 code, using only the standard library.

from urllib.request import urlopen
import json

URLs = [
    # Some URLs that return JSON objects
    'http://httpbin.org/ip',
    'http://httpbin.org/user-agent',
    'http://httpbin.org/headers'
]

json_list = []
for url in URLs:
    resp = urlopen(url)
    resp = resp.read().decode(resp.headers.get_content_charset() or 'ascii')
    json_list.append(json.loads(resp))
with open('result.json', 'w') as fp:
    json.dump(json_list, fp, indent=2)