Python Parsing JSON Url Example
The code below show an example of how to parse JSON data from a url using Python. In this case we are fetching current earthquake data from a url feed.
----------------------------------------------------
#Output
result code: 200
{'generated': 1555879611000, 'url': 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson', 'title': 'USGS Magnitude 2.5+ Earthquakes, Past Day', 'status': 200, 'api': '1.8.1', 'count': 42}
42 events recorded
36km NW of Valdez, Alaska
8km NW of Puerto Real, Puerto Rico
2km NNW of The Geysers, CA
73km W of San Antonio de los Cobres, Argentina
30km SSE of Jarm, Afghanistan
184km NNE of Road Town, British Virgin Islands
34km NNW of Grand Canyon, Arizona
93km NNW of Talkeetna, Alaska
69km S of Kobuk, Alaska
3km WSW of Volcano, Hawaii
87km N of Isabela, Puerto Rico
90km N of Isabela, Puerto Rico
57km NE of San Felipe, Chile
97km NNE of Punta Cana, Dominican Republic
16km WNW of Delta Junction, Alaska
61km SW of Sabtang, Philippines
25km SW of San Javier, Chile
52km SE of Ceva-i-Ra, Fiji
154km E of Kuril'sk, Russia
Bonin Islands, Japan region
150km SSE of Itoman, Japan
28km SE of Namlea, Indonesia
289km NNE of Ndoi Island, Fiji
41km SSW of Tanaga Volcano, Alaska
8km N of Maketu, New Zealand
269km SSE of Sigave, Wallis and Futuna
192km SSW of Bambanglipuro, Indonesia
7km NNW of Yuzawa, Japan
11km SSW of Sterling, Alaska
111km NNE of Vieques, Puerto Rico
4km WSW of Volcano, Hawaii
94km WSW of Nikol'skoye, Russia
51km SE of Pahala, Hawaii
196km NE of Palue, Indonesia
92km WSW of Nikol'skoye, Russia
44km SSW of Ovalle, Chile
92km WSW of Nikol'skoye, Russia
92km WSW of Nikol'skoye, Russia
131km WSW of Chernabura Island, Alaska
93km WSW of Nikol'skoye, Russia
46km SE of Tobelo, Indonesia
93km S of Amatignak Island, Alaska
-------------------------
5.0 73km W of San Antonio de los Cobres, Argentina
4.2 30km SSE of Jarm, Afghanistan
4.1 57km NE of San Felipe, Chile
4.6 61km SW of Sabtang, Philippines
4.9 25km SW of San Javier, Chile
5.4 52km SE of Ceva-i-Ra, Fiji
4.4 154km E of Kuril'sk, Russia
4.1 Bonin Islands, Japan region
4.7 150km SSE of Itoman, Japan
4.9 28km SE of Namlea, Indonesia
4.8 289km NNE of Ndoi Island, Fiji
4.9 41km SSW of Tanaga Volcano, Alaska
4.6 8km N of Maketu, New Zealand
4.9 269km SSE of Sigave, Wallis and Futuna
4.8 192km SSW of Bambanglipuro, Indonesia
4.5 7km NNW of Yuzawa, Japan
4.0 94km WSW of Nikol'skoye, Russia
4.6 196km NE of Palue, Indonesia
4.2 92km WSW of Nikol'skoye, Russia
4.3 44km SSW of Ovalle, Chile
4.8 92km WSW of Nikol'skoye, Russia
5.3 92km WSW of Nikol'skoye, Russia
4.8 93km WSW of Nikol'skoye, Russia
4.4 46km SE of Tobelo, Indonesia
-------------------------
Events that were felt
3.2 36km NW of Valdez, Alaska reported 1 times
3.1 2km NNW of The Geysers, CA reported 3 times
3.0 3km WSW of Volcano, Hawaii reported 2 times
4.9 25km SW of San Javier, Chile reported 25 times
4.6 8km N of Maketu, New Zealand reported 1 times
2.9 11km SSW of Sterling, Alaska reported 1 times
2.9 4km WSW of Volcano, Hawaii reported 3 times
# # Python parsing JSON url example # import urllib.request import json def printResults(data): # Use the json module to load the string data into a dictionary theJSON = json.loads(data) # now we can access the contents of the JSON like any other Python object if "title" in theJSON["metadata"]: print(theJSON["metadata"]) # output the number of events, plus the magnitude and each event name count = theJSON["metadata"]["count"] print(str(count) + " events recorded") # for each event, print the place where it occurred for i in theJSON["features"]: print(i["properties"]["place"]) print("-------------------------") # print the events that only have a magnitude greater than 4 for i in theJSON["features"]: if i["properties"]["mag"] >= 4.0: print("%2.1f" % i["properties"]["mag"], i["properties"]["place"]) print("-------------------------") # print only the events where at least 1 person reported feeling something print("Events that were felt") for i in theJSON["features"]: feltreports = i["properties"]["felt"] if feltreports != None: if feltreports > 0: print("%2.1f" % i["properties"]["mag"], i["properties"]["place"], " reported " + str(feltreports) + " times") def main(): # define a variable to hold the source URL # In this case we'll use the free data feed from the USGS # This feed lists all earthquakes for the last day larger than Mag 2.5 urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson" # Open the URL and read the data webUrl = urllib.request.urlopen(urlData) print ("result code: " + str(webUrl.getcode())) if webUrl.getcode() == 200: data = webUrl.read() printResults(data) else: print("Received error, cannot parse result") if __name__ == "__main__": main()
----------------------------------------------------
#Output
result code: 200
{'generated': 1555879611000, 'url': 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson', 'title': 'USGS Magnitude 2.5+ Earthquakes, Past Day', 'status': 200, 'api': '1.8.1', 'count': 42}
42 events recorded
36km NW of Valdez, Alaska
8km NW of Puerto Real, Puerto Rico
2km NNW of The Geysers, CA
73km W of San Antonio de los Cobres, Argentina
30km SSE of Jarm, Afghanistan
184km NNE of Road Town, British Virgin Islands
34km NNW of Grand Canyon, Arizona
93km NNW of Talkeetna, Alaska
69km S of Kobuk, Alaska
3km WSW of Volcano, Hawaii
87km N of Isabela, Puerto Rico
90km N of Isabela, Puerto Rico
57km NE of San Felipe, Chile
97km NNE of Punta Cana, Dominican Republic
16km WNW of Delta Junction, Alaska
61km SW of Sabtang, Philippines
25km SW of San Javier, Chile
52km SE of Ceva-i-Ra, Fiji
154km E of Kuril'sk, Russia
Bonin Islands, Japan region
150km SSE of Itoman, Japan
28km SE of Namlea, Indonesia
289km NNE of Ndoi Island, Fiji
41km SSW of Tanaga Volcano, Alaska
8km N of Maketu, New Zealand
269km SSE of Sigave, Wallis and Futuna
192km SSW of Bambanglipuro, Indonesia
7km NNW of Yuzawa, Japan
11km SSW of Sterling, Alaska
111km NNE of Vieques, Puerto Rico
4km WSW of Volcano, Hawaii
94km WSW of Nikol'skoye, Russia
51km SE of Pahala, Hawaii
196km NE of Palue, Indonesia
92km WSW of Nikol'skoye, Russia
44km SSW of Ovalle, Chile
92km WSW of Nikol'skoye, Russia
92km WSW of Nikol'skoye, Russia
131km WSW of Chernabura Island, Alaska
93km WSW of Nikol'skoye, Russia
46km SE of Tobelo, Indonesia
93km S of Amatignak Island, Alaska
-------------------------
5.0 73km W of San Antonio de los Cobres, Argentina
4.2 30km SSE of Jarm, Afghanistan
4.1 57km NE of San Felipe, Chile
4.6 61km SW of Sabtang, Philippines
4.9 25km SW of San Javier, Chile
5.4 52km SE of Ceva-i-Ra, Fiji
4.4 154km E of Kuril'sk, Russia
4.1 Bonin Islands, Japan region
4.7 150km SSE of Itoman, Japan
4.9 28km SE of Namlea, Indonesia
4.8 289km NNE of Ndoi Island, Fiji
4.9 41km SSW of Tanaga Volcano, Alaska
4.6 8km N of Maketu, New Zealand
4.9 269km SSE of Sigave, Wallis and Futuna
4.8 192km SSW of Bambanglipuro, Indonesia
4.5 7km NNW of Yuzawa, Japan
4.0 94km WSW of Nikol'skoye, Russia
4.6 196km NE of Palue, Indonesia
4.2 92km WSW of Nikol'skoye, Russia
4.3 44km SSW of Ovalle, Chile
4.8 92km WSW of Nikol'skoye, Russia
5.3 92km WSW of Nikol'skoye, Russia
4.8 93km WSW of Nikol'skoye, Russia
4.4 46km SE of Tobelo, Indonesia
-------------------------
Events that were felt
3.2 36km NW of Valdez, Alaska reported 1 times
3.1 2km NNW of The Geysers, CA reported 3 times
3.0 3km WSW of Volcano, Hawaii reported 2 times
4.9 25km SW of San Javier, Chile reported 25 times
4.6 8km N of Maketu, New Zealand reported 1 times
2.9 11km SSW of Sterling, Alaska reported 1 times
2.9 4km WSW of Volcano, Hawaii reported 3 times
Comments
Post a Comment