DEV Community

Cover image for Parsing "2-3 cloves garlic, minced" is harder than it looks
Dani
Dani

Posted on Edited on

Parsing "2-3 cloves garlic, minced" is harder than it looks

Ingredient lines look simple until you try to parse them. "2 1/2 cups chopped yellow onion" is easy enough, but real recipes also say "1 (14.5 oz) can diced tomatoes", "2-3 cloves garlic, minced" and "Salt to taste". A regex that handles one breaks on the next.

I ran into this while building recipescraper.pro. Shopping lists and unit conversions only work if you know which part of each line is the amount, the unit and the actual ingredient. So I turned that parsing step into its own API.

What it does

Send it an ingredient line like 2 1/2 cups chopped yellow onion and you get back structured JSON:

{
  "original": "2 1/2 cups chopped yellow onion",
  "quantity": 2.5,
  "quantity_max": null,
  "unit": "cup",
  "unit_type": "volume",
  "size": null,
  "ingredient": "yellow onion",
  "prep": "chopped",
  "notes": [],
  "metric": {"quantity": 591.5, "unit": "ml"}
}
Enter fullscreen mode Exit fullscreen mode

And the tricky ones come out clean too:

Line quantity unit ingredient extra
1 (14.5 oz) can diced tomatoes 1 can tomatoes size: 14.5 oz, prep: diced
2-3 cloves garlic, minced 2 (max 3) clove garlic prep: minced
Salt to taste null null Salt notes: to taste

It handles fractions (1/2, ½, 1½), ranges (2-3, 2 to 3), US and metric units, size words like large, and notes like optional or for garnish.

Quick example (Python)

import requests

resp = requests.post(
    "https://ingredient-parser5.p.rapidapi.com/parse-batch",
    json={"ingredients": [
        "2 1/2 cups chopped yellow onion",
        "2-3 cloves garlic, minced",
    ]},
    headers={
        "X-RapidAPI-Key": "your-key",
        "X-RapidAPI-Host": "ingredient-parser5.p.rapidapi.com",
    },
)
for item in resp.json()["results"]:
    print(item["quantity"], item["unit"], item["ingredient"])
Enter fullscreen mode Exit fullscreen mode

Use /parse for one line or /parse-batch for up to 100 at once.

What people use this for

  • Shopping lists that combine "1 cup onion" and "1/2 cup onion" into one line
  • Nutrition lookups, where you need the ingredient name separated from the amount
  • Pantry and meal-planning apps

Try it

There's a free plan, so you can throw your messiest ingredient lines at it without paying anything.

👉 Ingredient Parser on RapidAPI

Need the ingredient lines in the first place? My Recipe Extractor pulls them from any recipe link.

Found a line it gets wrong? Drop it in the comments. Those are the most useful bug reports I get.


More from Scraper Pro Tools: the Recipe Scaler scales recipes and converts US ↔ metric, and recipescraper.pro lets anyone grab a clean recipe from a link, no code needed.

Top comments (0)