Difference Between `json.dumps()` and `flask.jsonify()`

Naveera A.

The Problem

What is the difference between json.dumps() and flask.jsonify() and when should you use which?

The Solution

The jsonify() function is a part of the Flask framework whereas json.dumps() is a method in the built-in json package in Python.

The json.dumps() Method

If you are reading or writing JSON data from files, or in local memory, you should use the json package.

The json package offers many methods, including the json.dumps() method, to interchange Python data with JSON.

The json.dumps() method turns Python data, including dictionaries and lists, into JSON and returns this JSON as a string. For example:

import json books = [ {'name': 'The Call of the Wild', 'author':'Jack London'}, {'name': 'Heart of Darkness', 'author': 'Joseph Conrad'} ] json_string = json.dumps(books) print(json_string)

If we run the above code we will get the following output:

[{"name": "The Call of the Wild", "author": "Jack London"}, {"name": "Heart of Darkness", "author": "Joseph Conrad"}]

The flask.jsonify() Function

If you are using the Flask framework and want to send some data as an HTTP response, you should use the flask.jsonify() function.

The flask.jsonify() function returns a Response object. Flask serializes your data as JSON and adds it to this Response object. It also adds the appropriate mimetype by setting the content-type header field to application/json.

For example, if we want to return the list of books from the example above as an API response using Flask, we can write the following code:

from flask import Flask, jsonify app = Flask(__name__) @app.route('/books') def list_of_books(): books = [ {'name': 'The Call of the Wild', 'author': 'Jack London'}, {'name': 'Heart of Darkness', 'author': 'Joseph Conrad'} ] return jsonify(books) if __name__ == '__main__': app.run()

When we call the '/books' endpoint using curl, we get the following JSON data:

[ { "author": "Jack London", "name": "The Call of the Wild" }, { "author": "Joseph Conrad", "name": "Heart of Darkness" } ]
Join the discussionCome work with us
Share on Twitter
Bookmark this page
Ask a questionImprove this Answer

Related Answers

A better experience for your users. An easier life for your developers.

© 2023 • Sentry is a registered Trademark
of Functional Software, Inc.