Use jsonify() instead of json.dumps() in Flask
Last Updated :
23 Jul, 2025
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.
Python
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.
Python
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.
Python
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:
jsonify() method 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.
Python
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
Snapshot of the development /api/users endpointWhy 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:
Similar Reads
json.dumps() in Python json.dumps() is a function in Pythonâs json module that converts a Python object into a JSON formatted string. It allows you to serialize Python objects such as dictionaries, lists, and more into JSON format.Syntaxjson.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True
5 min read
json.dumps() in Python json.dumps() is a function in Pythonâs json module that converts a Python object into a JSON formatted string. It allows you to serialize Python objects such as dictionaries, lists, and more into JSON format.Syntaxjson.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True
5 min read
json.dumps() in Python json.dumps() is a function in Pythonâs json module that converts a Python object into a JSON formatted string. It allows you to serialize Python objects such as dictionaries, lists, and more into JSON format.Syntaxjson.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True
5 min read
Append to JSON file using Python JSON (JavaScript Object Notation) is a lightweight data format that stores data as key-value pairs within curly braces {}. Python's json module makes it easy to work with JSON data, whether you are parsing JSON strings, converting Python objects to JSON, or appending new data to existing JSON files.
2 min read
Append to JSON file using Python JSON (JavaScript Object Notation) is a lightweight data format that stores data as key-value pairs within curly braces {}. Python's json module makes it easy to work with JSON data, whether you are parsing JSON strings, converting Python objects to JSON, or appending new data to existing JSON files.
2 min read
Append to JSON file using Python JSON (JavaScript Object Notation) is a lightweight data format that stores data as key-value pairs within curly braces {}. Python's json module makes it easy to work with JSON data, whether you are parsing JSON strings, converting Python objects to JSON, or appending new data to existing JSON files.
2 min read