Food API JSON Macros Per 100g: Structured Response Guide
Published May 24, 2026
Inconsistent JSON breaks apps. A food API JSON macros per 100g structured response standard lets every client scale portions the same way. Calorie API returns predictable fields across search, detail, and barcode endpoints.
Standard Macro Fields
| JSON field | Unit | Meaning |
|---|---|---|
calories | kcal | Energy per 100g |
protein_g | grams | Protein per 100g |
carbohydrates_g | grams | Carbs per 100g |
fat_g | grams | Fat per 100g |
fiber_g | grams | Fiber per 100g (when present) |
sugars_g | grams | Sugars per 100g (when present) |
Values are per 100g unless using portion calc for scaled servings.
TypeScript Interface
export type FoodMacrosPer100g = {
id: number;
name: string;
brand?: string | null;
calories: number | null;
protein_g: number | null;
carbohydrates_g: number | null;
fat_g: number | null;
fiber_g?: number | null;
sugars_g?: number | null;
};
Search vs Detail
| Endpoint | Macro completeness |
|---|---|
/search/foods | Common macros on rows |
/foods/{id} | Full nutrient profile |
/search/barcode/{upc} | Macros + allergens |
Fetch detail after user selects a food for logging.
Scaling Pattern
function scale(per100g, grams) {
const r = grams / 100;
return {
calories: (per100g.calories ?? 0) * r,
protein_g: (per100g.protein_g ?? 0) * r,
carbohydrates_g: (per100g.carbohydrates_g ?? 0) * r,
fat_g: (per100g.fat_g ?? 0) * r,
};
}
Full guide: scale per 100g to grams.
Null Handling
Never treat null as zero in UI. Show N/A and block log until user confirms.
Related
Frequently Asked Questions
Are Calorie API macros always per 100g?
Yes on search, detail, and barcode responses unless you use portion calc which returns values scaled to the requested gram amount.
What JSON fields contain macros?
calories, protein_g, carbohydrates_g, fat_g, and optionally fiber_g and sugars_g per 100g.
How do I scale JSON macros to a serving?
Multiply each per-100g field by (grams / 100) or call GET /public/calc/portion with food_id and grams.
Why are some macro fields null?
Incomplete source data for some foods. Handle nulls in UI rather than defaulting to zero.
Does barcode JSON use the same macro structure?
Yes. Barcode lookup returns the same per-100g macro fields plus barcode and allergen arrays when available.
