how to return json response in flask
To return a JSON response in Flask, you can follow these steps:
Import the necessary modules:
python from flask import Flask, jsonify
Create a Flask app:
python app = Flask(__name__)
Define a route that returns a JSON response:
python @app.route('/get_json', methods=['GET']) def get_json(): data = {'key1': 'value1', 'key2': 'value2'} return jsonify(data)
Run the Flask app:
python if __name__ == '__main__': app.run()