SOLVED: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) |raise TypeError(f’Object of type {o.class.name} ‘ TypeError: Object of type set is not JSON serializable in Python

Error

The error appears in Python like below on the screen –

raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

&

raise TypeError(f’Object of type {o.class.name} ‘
TypeError: Object of type set is not JSON serializable

Solution

This error is coming because ,

Data is not getting return in JSON format.

To read & understand more about JSON Format refer – https://www.w3schools.com/whatis/whatis_json.asp

As you could see below in code –

from flask import Flask
from flask_restful import Api, Resource

app=Flask(__name__)
api =Api(app)

class HelloWorld(Resource):
    def get(self):
        return {"Hello World"}

api.add_resource(HelloWorld,"/helloworld")

if __name__ == "__main__":
    app.run(debug=True)

In above code at 7th line , We are returning the data like –

return {"Hello World"} #Wrong Way

which is wrong & it’s the main cause of Error , correct it with the right way of giving data in JSON Format like –

return {"data":"Hello World"} #right way

Correct this & your error will be resolved.