Ajay Rawat Ajay Rawat - 3 months ago 22
JSON Question

builtins.TypeError TypeError: {2, 5, 23} is not JSON serializable

# This is my code , i am getting

JSON
not serializable error in
b.start_time
.

def rooms():
rooms = Room.query.all()
formatted_rooms = []
for r in rooms:
book = Booking.query.filter_by(room_id=r.id).all()
formatted_rooms.append( {
'name' : r.name,
'floor': r.floor, 'location': r.location,
'city' : r.city, 'capacity' : r.capacity,
'credits' : r.credits, 'amenities' : r.amenities ,
'room_id' : r.id,
'booking_details': { (b.start_time)
for b in book }
} )
return json.dumps(formatted_rooms) , 200, {'Content-Type':'application/json'}

Answer

A set is not a JSON data-type

Change this

'booking_details': { (b.start_time) for b in book }

To a list

'booking_details': [ (b.start_time) for b in book ]