i'm trying to implement a JSON API using Flask and marshmallow but I'm struggling to send a response that looks like the following snippet by using a marshmallow schema.
{
"recipeNames":
[
"scrambledEggs",
"garlicPasta",
"chai"
]
}
My data looks like this (I'm using a json file as a database)
{
"recipes": [
{
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
],
"name": "scrambledEggs"
},
{
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
],
"name": "garlicPasta"
},
{
"ingredients": [
"400mL water",
"100mL milk",
"5g chai masala",
"2 tea bags or 20 g loose tea leaves"
],
"instructions": [
"Heat water until 80 C",
"Add milk, heat until 80 C",
"Add tea leaves/tea bags, chai masala; mix and steep for 3-4 minutes",
"Remove mixture from heat; strain and enjoy"
],
"name": "chai"
},
{
"ingredients": [
"1 bagel",
"2 tbsp butter"
],
"instructions": [
"cut the bagel",
"spread butter on bagel"
],
"name": "butteredBagel"
}
]}
And i'm using a marshmallow schema that looks like this
class RecipeSchema(Schema):
ingredients = fields.List(fields.Str())
instructions = fields.List(fields.Str())
name = fields.Str()
So far, I tried to use another schema completely just to have a single attribute named "recipeNames", but it feels wrong to implement a different schema for every variation of response we can send with the API. I also tried just not using marshmallow, which worked, but then I can't really validate the data while it's transiting through the API. Finally I tried using some context or some extension like pre_dump by reading the marshmallow doc and examples Marshmallow examples, Context Doc, but now I am kind of lost on what would be the best way to achieve what I'm trying to do.
I guess this would be very close to doing aggregation after the data is loaded into the schema, but I couldn't found any documentation on such a thing in marshmallow. Did anyone ever had this kind of problem?
EDIT
Maybe this question can help Multiple Variants of schemas, but I'm still not sure how to handle what I want to achieve