how to return json response in flask

To return a JSON response in Flask, you can follow these steps:

  1. Import the necessary modules: python from flask import Flask, jsonify

  2. Create a Flask app: python app = Flask(__name__)

  3. 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)

  4. Run the Flask app: python if __name__ == '__main__':