How to Send JSON Response in Flask: Simple Guide
In Flask, you can send a JSON response using the
jsonify() function, which converts your data into JSON format and sets the correct content type. Alternatively, you can use the Response object with json.dumps() and set the Content-Type header to application/json.Syntax
The most common way to send JSON in Flask is using jsonify(). It takes Python dictionaries or lists and returns a Flask Response object with JSON data and the correct Content-Type header.
Alternatively, you can manually create a Response object by converting your data to a JSON string with json.dumps() and setting the Content-Type header to application/json.
python
from flask import jsonify, Response import json # Using jsonify jsonify({'key': 'value'}) # Using Response Response(json.dumps({'key': 'value'}), mimetype='application/json')
Example
This example shows a simple Flask app with a route that returns a JSON response using jsonify(). When you visit /data, it sends a JSON object with a message and a number.
python
from flask import Flask, jsonify app = Flask(__name__) @app.route('/data') def data(): response_data = {'message': 'Hello, JSON!', 'number': 123} return jsonify(response_data) if __name__ == '__main__': app.run(debug=True)
Output
{"message":"Hello, JSON!","number":123}
Common Pitfalls
- Not using
jsonify()or setting theContent-Typeheader causes the client to not recognize the response as JSON. - Returning a Python dictionary directly without
jsonify()orResponsewill not send JSON properly in Flask versions before 1.1. - Using
json.dumps()without settingmimetype='application/json'leads to incorrect content type.
python
from flask import Flask import json app = Flask(__name__) @app.route('/wrong') def wrong(): # This returns a Python dict, not JSON response in Flask <1.1 return {'message': 'This will cause an error or unexpected behavior'} @app.route('/right') def right(): # Correct way using jsonify from flask import jsonify return jsonify({'message': 'This is correct'}) @app.route('/manual') def manual(): from flask import Response return Response(json.dumps({'message': 'Manual JSON'}), mimetype='application/json')
Quick Reference
Use this quick reference to remember how to send JSON responses in Flask:
| Method | Usage | Notes |
|---|---|---|
| jsonify() | jsonify({'key': 'value'}) | Recommended, sets content type automatically |
| Response + json.dumps() | Response(json.dumps(data), mimetype='application/json') | Manual control, remember mimetype |
| Returning dict directly | return {'key': 'value'} | Works in Flask 1.1+, but use jsonify for clarity |
Key Takeaways
Use Flask's jsonify() to send JSON responses easily with correct headers.
Always ensure the Content-Type header is set to application/json for JSON responses.
Avoid returning raw Python dictionaries without jsonify or Response in Flask versions before 1.1.
You can manually create JSON responses using Response and json.dumps() if needed.
Flask 1.1+ allows returning dicts directly, but jsonify is clearer and safer.