apod.json
and add the following to it:{ "copyright": "Yin Hao", "date": "2018-10-30", "explanation": "Meteors have been shooting out from the constellation of Orion. This was expected, as October is the time of year for the Orionids Meteor Shower. Pictured here, over two dozen meteors were caught in successively added exposures last October over Wulan Hada volcano in Inner Mongolia, China. The featured image shows multiple meteor streaks that can all be connected to a single small region on the sky called the radiant, here visible just above and to the left of the belt of Orion, The Orionids meteors started as sand sized bits expelled from Comet Halley during one of its trips to the inner Solar System. Comet Halley is actually responsible for two known meteor showers, the other known as the Eta Aquarids and visible every May. An Orionids image featured on APOD one year ago today from the same location shows the same car. Next month, the Leonids Meteor Shower from Comet Tempel-Tuttle should also result in some bright meteor streaks. Follow APOD on: Facebook, Instagram, Reddit, or Twitter", "hdurl": "https://apod.nasa.gov/apod/image/1810/Orionids_Hao_2324.jpg", "media_type": "image", "service_version": "v1", "title": "Orionids Meteors over Inner Mongolia", "url": "https://apod.nasa.gov/apod/image/1810/Orionids_Hao_960.jpg" }
test.py
and paste the following code into it to decode the JSON in our apod.json
text file, store it in a Python dictionary, and then decode it back into a string:import json with open('apod.json', 'r') as f: json_text = f.read() # Decode the JSON string into a Python dictionary. apod_dict = json.loads(json_text) print(apod_dict['explanation']) # Encode the Python dictionary into a JSON string. new_json_string = json.dumps(apod_dict, indent=4) print(new_json_string)
python test.py
pip install simplejson==3.16.0
simplejson
module, give it the name json
, and the rest of the code from the previous example should just work.import simplejson as json with open('apod.json', 'r') as f: json_text = f.read() # Decode the JSON string into a Python dictionary. apod_dict = json.loads(json_text) print(apod_dict['explanation']) # Encode the Python dictionary into a JSON string. new_json_string = json.dumps(apod_dict, indent=4) print(new_json_string)
python test.py
pip install ujson==1.35
import ujson as json with open('apod.json', 'r') as f: json_text = f.read() # Decode the JSON string into a Python dictionary. apod_dict = json.loads(json_text) print(apod_dict['explanation']) # Encode the Python dictionary into a JSON string. new_json_string = json.dumps(apod_dict, indent=4) print(new_json_string)
python test.py
json()
method on the response object that is returned when your HTTP request is finished. It's great to have a built in solution so you don't have to import more libraries for a simple task.pip install requests==2.20.0
.json
file from the other examples.apod.py
and add the following code to it:import requests apod_url = 'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY' apod_dict = requests.get(apod_url).json() print(apod_dict['explanation'])
GET
request to NASA's API, parses the JSON data that it returns using this built in method, and prints out the explanation of the current Astronomy Picture of the Day.python apod.py
pip install flask==1.0.2
app.py
, where the code for our example web app will live:from flask import Flask, jsonify app = Flask(__name__) @app.route('/apod', methods=['GET']) def apod(): url = 'https://apod.nasa.gov/apod/image/1810/Orionids_Hao_960.jpg' title = 'Orionids Meteors over Inner Mongolia' return jsonify(url=url, title=title) if __name__ == '__main__': app.run()
/apod
, and anytime a GET
request is sent to that route, the apod()
function is called. In this function, we are pretending to respond with the Astronomy Picture of the Day. In this example the data we're returning is just hard coded, but you can replace this with data from any other source.python app.py
, and then visit http://localhost:5000/apod in your browser to see the JSON data.jsonify
function takes data in the form of: