Use jsonify() instead of json.dumps() in Flask
Last Updated :
26 Apr, 2025
Here, we will understand the jsonify() function in the Flask web framework for Python that converts the output of a function to a JSON response object. It is similar to the json.dumps() function in the Python standard library, which converts a Python object to a JSON-formatted string.
What is jsonify()
The jsonify() function is useful in Flask apps because it automatically sets the correct response headers and content type for JSON responses, and allows you to easily return JSON-formatted data from your route handlers. This makes it easier and more convenient to create APIs that return JSON data.
Syntax of jsonify() function
This function takes in one or more positional arguments, which represent the data to be converted to a JSON response object, and any number of keyword arguments, which are used to customize the JSON response object.
jsonify(*args, **kwargs)
Example of jsonify() with without argument
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.
Python3
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/api/users')
def get_users():
return jsonify()
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. The status argument is used to set the HTTP status code for the response, and the mimetype argument is used to set the content type for the response.