I have a table is Database similar to below data. I am trying to create a REST API using Flask-Restful.
I am using flask-restful and flask-sqlalchemy with the below api.
/project/1
@classmethod
def find_by_project_no(cls,proj_no):
cls.query.filter_by(project_no = proj_no).all()
Result is [<automap.bla.bla>,<>,<>...]
I need the result converted into.
{
'project': [{
'country': 'USA',
'category': [{
'Beverages': [{
'product': 'Milk',
'count': 2
}, {
'product': 'Juice',
'count': 5
}
]
}, {
'Snacks': [{
'product': 'Potato Chips',
'count': 2
}
]
}, {
'Oils': [{
'product': 'Canola',
'count': 5
}, {
'product': 'Olive',
'count': 8
}
]
}
]
}, {
'country': 'CAN',
'category': [{
'Beverages': [{
'product': 'Milk',
'count': 7
}, {
'product': 'Juice',
'count': 8
}
]
}, {
'Snacks': [{
'product': 'Potato Chips',
'count': 8
}
]
}, {
'Oils': [{
'product': 'Canola',
'count': 3
}, {
'product': 'Olive',
'count': 4
}
]
}
]
}
]
}
How can I get the desired json format? Any help is appreciated.