When developing APIs with Flask, you will often need to send JSON responses. Although both jsonify() and json.dumps() can be used for this, Flask provides the jsonify() helper function for a more seamless experience.
What is jsonify()
jsonify() is a built-in Flask function that converts Python dictionaries and objects into JSON response objects. It also automatically sets:
- The correct Content-Type header (application/json)
- Proper HTTP status codes (default: 200 OK)
This makes jsonify() more convenient than manually using json.dumps() with Response.
Syntax of jsonify()
jsonify(*args, **kwargs)
- args: Single list or dict (will be JSON-encoded)
- kwargs: Named key-value pairs, returned as JSON object
Examples of jsonify()
Let's look at some of the examples and use cases for jsonify().
Example 1: Using jsonify() Without Arguments
You can use jsonify() without any arguments, in this case it will return an empty JSON response object with a default status code of 200 (OK) and a default content type of application/json.
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/users')
def get_users():
return jsonify()
Output:
{}
Explanation:
- This creates an empty JSON response ({}) with HTTP status 200 OK.
- Useful for routes that acknowledge a request without returning data.
Example 2: Using jsonify() With Arguments
In this example, we are calling jsonify() with a single positional argument (the list of user objects), as well as two keyword arguments with status and mimetype.
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/users')
def get_users():
users = [{'id': 1, 'username': 'Prajjwal'}, {'id': 2, 'username': 'Kareena'}]
return jsonify(users, status=200, mimetype='application/json')
Output:
{
"users": [
{"id": 1, "username": "Alice"},
{"id": 2, "username": "Bob"}
]
}
Explanation:
- A list of user dictionaries is passed as a named argument (users=...).
- jsonify() wraps it as a JSON object.
Full Flask App Using jsonify()
In this example, we have a Flask app with a route that returns a list of user objects. When a client makes a request to this route, the get_users() function is executed and the list of user objects is converted to a JSON response object using the jsonify() function. This JSON response object is then sent back to the client.
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def get_users():
print("Returning JSON data using jsonify()")
users = [
{'id': 1, 'username': 'sweety'},
{'id': 2, 'username': 'pallavi'}
]
return jsonify({'users': users})
if __name__ == '__main__':
app.run(debug=True)
Output:

Explanation:
- '/' route returns a JSON object with a list of users.
- debug=True enables live reloading and error reporting during development.
- jsonify() handles all the boilerplate like content-type and encoding.
json.dumps() method in Flask
In contrast, if you were to use the json.dumps() function, you would need to convert the list of user objects to a JSON-formatted string yourself, and then set the appropriate response headers and return the response to the client manually:
Using jsonify() is generally easier and more convenient than using json.dumps(), so it's recommended to use jsonify() whenever possible in Flask apps.
from flask import Flask, Response
import json
app = Flask(__name__)
@app.route('/api/users')
def get_users():
users = [{'id': 1, 'username': 'sweety'},
{'id': 2, 'username': 'pandey'}]
response = Response(
response=json.dumps(users),
status=200,
mimetype='application/json'
)
return response
if __name__ == "__main__":
app.run()
Visit URL: http://127.0.0.1:5000/api/users

Why to Use jsonify() instead of json.dumps()
There are several reasons why it is recommended to use the jsonify() function instead of the json.dumps() function in Flask apps:
- Automatically sets proper headers
- Simple and readable syntax
- Reduces manual errors
- Flask-native and tightly integrated
- Ideal for RESTful APIs and AJAX responses
Related Articles: