Sentry Answers>Flask>

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

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:

Click to Copy
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:

Click to Copy
[{"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:

Click to Copy
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:

Click to Copy
[ { "author": "Jack London", "name": "The Call of the Wild" }, { "author": "Joseph Conrad", "name": "Heart of Darkness" } ]
  • SentryFlask Error Monitoring
  • Syntax.fmListen to the Syntax Podcast
  • Community SeriesIdentify, Trace, and Fix Endpoint Regression Issues
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

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