Voorbeeld JSON parsing met RestAssured
gebruikte API in dit voorbeeld: https://rapidapi.com/Murad123/api/recipe-food-nutrition15
Voorbeeld code
In onderstaand voorbeeld wordt gedemonstreerd hoe een endpoint uit te lezen is en de JSON (array) data automatisch geconverteerd kan worden naar object.
@JsonIgnoreProperties(ignoreUnknown = true)
public record Recipe(
@JsonProperty("label")
String label,
@JsonProperty("ingredientLines")
List<String> ingredientLines) {
}
public class RecipeFoodNutritionDriver {
private static String BASE_URI = "https://recipe-food-nutrition15.p.rapidapi.com";
private static String HOST = "recipe-food-nutrition15.p.rapidapi.com";
private static String KEY = "<INSERT_OWN_KEY>";
public List<Recipe> getRecipes(String query) {
return RestAssured
.given()
.baseUri(BASE_URI)
.header("x-rapidapi-host", HOST)
.header("x-rapidapi-key", KEY)
.contentType(ContentType.JSON)
.body(String.format("{\"query\":\"%s\"}", query))
.when()
.post("recipee-search")
.then()
.statusCode(200)
.extract()
.jsonPath()
.getList("hits.recipe", Recipe.class);
}
}
Voorbeeld HTTP response van de API
{
"q": "coffee and croissant",
"from": 0,
"to": 2,
"more": true,
"count": 18,
"hits": [
{
"recipe": {
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_fa0c4ebb7875f75b2f15fb2ee5f87240",
"label": "Coffee Stout and Chocolate Bread Pudding",
"image": "https://edamam-product-images.s3.amazonaws.com/web-img/8c1/8c1cb0d2350a5dc8d5ad5ce62ee14ddb.jpg?X-Amz-Security-Token=IQoJb3JpZ2luX2VjELD%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJFMEMCHxw7nLVV%2Fpc3lp3w1b1h0HQXRhxy%2FwvsLsZEVCVvMPkCIGNRAalEYe%2B3gWC06%2BGAfzy8hji%2Bf1CFwEnwsgqCXnI3KrgFCHgQABoMMTg3MDE3MTUwOTg2IgwM6gzgruklFFjFSP4qlQUo3G1wJeoM75X54AUNrJOM4JN1Qn4%2BNhGJhvQDY7bwSjtlgxPb2tZYe1ocd6oPV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fn62bUjSqyNb%2FN9M%2FfnCvVoG3ErnT3WukLYPDlAsX1U2JQxi6LQ%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20241219T155646Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Credential=ASIASXCYXIIFHUYJHGUO%2F20241219%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=f139bf1c90215788b671027885744d7d97e1a5de2b0f54abfc247c8790196039",
"source": "craftbeer.com",
"url": "https://www.craftbeer.com/recipes/coffee-stout-chocolate-bread-pudding",
"shareAs": "http://www.edamam.com/recipe/coffee-stout-and-chocolate-bread-pudding-fa0c4ebb7875f75b2f15fb2ee5f87240/coffee+and+croissant",
"yield": 10,
"dietLabels": [],
"healthLabels": [
"Low Potassium",
"Kidney-Friendly",
"Vegetarian",
"Pescatarian",
"Peanut-Free",
"Tree-Nut-Free",
"Soy-Free",
"Fish-Free",
"Shellfish-Free",
"Pork-Free",
"Red-Meat-Free",
"Crustacean-Free",
"Celery-Free",
"Mustard-Free",
"Sesame-Free",
"Lupine-Free",
"Mollusk-Free",
"Kosher"
],
"cautions": [
"Sulfites"
],
"ingredientLines": [
"1 package of croissant dough (I prefer Wewalka as it’s GMO free)",
"4 oz of bittersweet chocolate, roughly chopped",
"1 cup of coffee stout",
"1 cup of heavy cream",
"1/2 vanilla bean",
"Pinch of salt",
"1/3 cup of sugar",
"3 egg yolks"
],
"ingredients": [
{
"text": "1 package of croissant dough (I prefer Wewalka as it’s GMO free)",
"quantity": 1,
"measure": "package",
"food": "dough",
"weight": 228,
"foodCategory": "bread, rolls and tortillas",
"image": "https://www.edamam.com/food-img/19f/19fc0706f04d44f62fc79ab4d7a07d7b.jpg"
},
{
"text": "4 oz of bittersweet chocolate, roughly chopped",
"quantity": 4,
"measure": "ounce",
"food": "bittersweet chocolate",
"weight": 113.3980925,
"foodCategory": "chocolate",
"image": "https://www.edamam.com/food-img/0a1/0a1b6f30cb99e1842cac6167f6f424d7.jpg"
},
{
"text": "1 cup of coffee stout",
"quantity": 1,
"measure": "cup",
"food": "stout",
"weight": 237.6,
"foodCategory": "beer",
"image": "https://www.edamam.com/food-img/7f8/7f82e3944cd8fcba3ffad7b6ce741c62.jpg"
},
{
"text": "1 cup of heavy cream",
"quantity": 1,
"measure": "cup",
"food": "heavy cream",
"weight": 238,
"foodCategory": "Dairy",
"image": "https://www.edamam.com/food-img/484/4848d71f6a14dd5076083f5e17925420.jpg"
},
{
"text": "1/2 vanilla bean",
"quantity": 0.5,
"measure": "<unit>",
"food": "vanilla bean",
"weight": 2.5,
"foodCategory": "Condiments and sauces",
"image": "https://www.edamam.com/food-img/90f/90f910b0bf82750d4f6528263e014cca.jpg"
},
{
"text": "Pinch of salt",
"quantity": 1,
"measure": "pinch",
"food": "salt",
"weight": 0.380208334,
"foodCategory": "Condiments and sauces",
"image": "https://www.edamam.com/food-img/694/6943ea510918c6025795e8dc6e6eaaeb.jpg"
},
{
"text": "1/3 cup of sugar",
"quantity": 0.3333333333333333,
"measure": "cup",
"food": "sugar",
"weight": 66.66666666666666,
"foodCategory": "sugars",
"image": "https://www.edamam.com/food-img/ecb/ecb3f5aaed96d0188c21b8369be07765.jpg"
},
{
"text": "3 egg yolks",
"quantity": 3,
"measure": "<unit>",
"food": "egg yolks",
"weight": 40.4,
"foodCategory": "Eggs",
"image": "https://www.edamam.com/food-img/e3f/e3f697887aec0e8ecf09a9ca8ef3944a.jpg"
}
]
}
},
{
"recipe": {
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_1a097bc3b56d5de504894e5f38c9fc07",
"label": "Hazelnut Croissant Toast With Nutella Butter Recipe",
"image": "https://edamam-product-images.s3.amazonaws.com/web-img/572/5720315eef84811d5bcfadaf516b4692.jpg?X-Amz-Security-Token=IQoJb3JpZ2luX2VjELD%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJFMEMCHxw7nLVV%2Fpc3lp3w1b1h0HQXRhxy%2FwvsLs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jriDIElV4fDCkmF93q9NHI8boWGof3T5caqabdSySQFrmZlZY1lMnv%2FhslKv3wmWyZ1YTYtf5l3Dp0%2BztSxw8Km0vWRCLX%2Fg%2FcG3jt%2FDQQhPOlUuyvHcRWsLnJ%2F7IscyR9ucEAmNCtl%2BNBgMQjX6jT3InmvYR9TB%2Fn62bUjSqyNb%2FN9M%2FfnCvVoG3ErnT3WukLYPDlAsX1U2JQxi6LQ%3D&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20241219T155646Z&X-Amz-SignedHeaders=host&X-Amz-Expires=3600&X-Amz-Credential=ASIASXCYXIIFHUYJHGUO%2F20241219%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=881099e62da4a2aaf11c8d4f912b1c095b8292c146326140244502be291b0e77",
"source": "Group Recipes",
"url": "http://www.grouprecipes.com/128699/hazelnut-croissant-toast-with-nutella-butter.html",
"shareAs": "http://www.edamam.com/recipe/hazelnut-croissant-toast-with-nutella-butter-recipe-1a097bc3b56d5de504894e5f38c9fc07/coffee+and+croissant",
"yield": 6,
"dietLabels": [],
"healthLabels": [
"Kidney-Friendly",
"Vegetarian",
"Pescatarian",
"Peanut-Free",
"Fish-Free",
"Shellfish-Free",
"Pork-Free",
"Red-Meat-Free",
"Crustacean-Free",
"Celery-Free",
"Mustard-Free",
"Sesame-Free",
"Lupine-Free",
"Mollusk-Free",
"Alcohol-Free",
"Sulfite-Free",
"Kosher"
],
"cautions": [
"Sulfites"
],
"ingredientLines": [
"6 full sized or 12 mini croissants, whole, do not slice or cut",
"4 eggs",
"1t liquid hazelnut coffee creamer or 1t dry creamer with 1t half and half added",
"About 3t butter for frying",
"A pinch of fine sea salt",
"For nutella butter",
"About 1/2 cup nutella",
"1t butter(no subs)"
],
"ingredients": [
{
"text": "6 full sized or 12 mini croissants, whole, do not slice or cut",
"quantity": 6,
"measure": "<unit>",
"food": "croissants",
"weight": 168,
"foodCategory": "bread, rolls and tortillas",
"image": "https://www.edamam.com/food-img/590/590b8e57dd922af23adc3b4578a9033a.gif"
},
{
"text": "4 eggs",
"quantity": 4,
"measure": "<unit>",
"food": "eggs",
"weight": 172,
"foodCategory": "Eggs",
"image": "https://www.edamam.com/food-img/a7e/a7ec7c337cb47c6550b3b118e357f077.jpg"
},
{
"text": "1t liquid hazelnut coffee creamer or 1t dry creamer with 1t half and half added",
"quantity": 1,
"measure": "teaspoon",
"food": "coffee creamer",
"weight": 5.00000000033814,
"foodCategory": "plant-based protein",
"image": "https://www.edamam.com/food-img/36d/36d0342199dc45df868960c3938d3b29.jpg"
},
{
"text": "About 3t butter for frying",
"quantity": 3,
"measure": "teaspoon",
"food": "butter",
"weight": 14.187500000719606,
"foodCategory": "Dairy",
"image": "https://www.edamam.com/food-img/713/71397239b670d88c04faa8d05035cab4.jpg"
},
{
"text": "A pinch of fine sea salt",
"quantity": 1,
"measure": "pinch",
"food": "fine sea salt",
"weight": 0.380208334,
"foodCategory": "Condiments and sauces",
"image": "https://www.edamam.com/food-img/694/6943ea510918c6025795e8dc6e6eaaeb.jpg"
},
{
"text": "For nutella butter",
"quantity": 0,
"measure": null,
"food": "butter",
"weight": 6.967237500026714,
"foodCategory": "Dairy",
"image": "https://www.edamam.com/food-img/713/71397239b670d88c04faa8d05035cab4.jpg"
},
{
"text": "About 1/2 cup nutella",
"quantity": 0.5,
"measure": "cup",
"food": "nutella",
"weight": 148,
"foodCategory": "chocolate",
"image": "https://www.edamam.com/food-img/375/37567989ef8d6f7392094111de56f62b.jpg"
},
{
"text": "1t butter(no subs)",
"quantity": 1,
"measure": "teaspoon",
"food": "butter",
"weight": 4.7291666669065355,
"foodCategory": "Dairy",
"image": "https://www.edamam.com/food-img/713/71397239b670d88c04faa8d05035cab4.jpg"
}
]
}
}
]
}