I have a flask API that consumes SQL queries using SQL Alchemy
Sometimes I have objects in my array that contains null values, I would like to remove them but havent been succesful in my attempts..
class SliceData(Resource):
def get(self, axis_choice, legend_choice, year_choice):
connection = db_connect.connect()
query = select([
getattr(workers.c, axis_choice),
getattr(workers.c, legend_choice),
func.count(workers.c.id).label('count')
])
query = query.where(workers.c.year == year_choice)
query = query.group_by( getattr(workers.c, legend_choice), getattr(workers.c, axis_choice))
result = connection.execute(query)
return jsonify({'data': [dict(row) for row in result]})
The code above outputs JSON data that looks like this
{
"data": [
{
"age": 46,
"count": 33,
"years_training": 2
},
{
"age": 32,
"count": 67,
"years_training": 0
},
{
"age": 51,
"count": 1262,
"years_training": null
},
]
}
I have tried getting rid of null values using SQLAlchemy with query = query.where(workers.c.years_training !=None)
but that ends up changing the query
So I would like to remove the objects that have null values in my return jsonify
so that my output looks like this
{
"data": [
{
"age": 46,
"count": 33,
"years_training": 2
},
{
"age": 32,
"count": 67,
"years_training": 0
},
]
}