close

[Solved] Flask API TypeError: Object of type ‘Response’ is not JSON serializable

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Flask API TypeError: Object of type ‘Response’ is not JSON serializable in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Flask API TypeError: Object of type ‘Response’ is not JSON serializable Error Occurs?

Today I get the following error Flask API TypeError: Object of type ‘Response’ is not JSON serializable in python.

How To Solve Flask API TypeError: Object of type ‘Response’ is not JSON serializable Error ?

  1. How To Solve Flask API TypeError: Object of type 'Response' is not JSON serializable Error ?

    To Solve Flask API TypeError: Object of type 'Response' is not JSON serializable ErrorInspired from this bug, here is a shorter way of doing it:
    from flask import jsonify, make_response

  2. Flask API TypeError: Object of type 'Response' is not JSON serializable

    To Solve Flask API TypeError: Object of type 'Response' is not JSON serializable ErrorInspired from this bug, here is a shorter way of doing it:
    from flask import jsonify, make_response

Solution 1

Inspired from this bug, here is a shorter way of doing it:

from flask import jsonify, make_response

def myMethod():
    ....
    return make_response(jsonify(data), 200)

Solution 2

This can be simply done by:

from flask import jsonify
def myMethod():
    ....
    response = jsonify(data)
    response.status_code = 200 # or 400 or whatever
    return response

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read