A dynamic key-value map stores arbitrary pairs whose keys and structure are not known when an API schema is defined. Use it for dynamic data, not merely to regroup fields that already have a known shape. section 5.7
Choose between a map and a custom data type deliberately:
- Use a custom data type when related fields are known, typed, and worth organizing under one field. section 5.7
- Use a map when the data is arbitrary, dynamic, and has no expected structure. section 5.7
Ingredient amounts
A GroceryItem can expose ingredientAmounts as Map<string, string>, allowing entries such as fat, sodium, carbohydrate, sugar, fiber, and protein without adding a fixed field for every ingredient. section 5.7
Listing 5.10 Example JSON representation of an ingredients map
{
"id": "514a0119-bc3f-4e3f-9a64-8ad48600c5d8",
"name": "Pringles",
"calories": "150",
"ingredientAmounts": {
"fat": "9 g",
"sodium": "150 mg",
"carbohydrate": "15 g",
"sugar": "1 g",
"fiber": "1 g",
"protein": "1 g"
}
}
Inspect a JSON representation of an ingredients map and its arbitrary keys and values. section 5.7
