Skip to content

Instantly share code, notes, and snippets.

@hadley
Created November 18, 2024 22:29
Show Gist options
  • Save hadley/7688b4dd1e5e97b800c6d7d79e437b48 to your computer and use it in GitHub Desktop.
Save hadley/7688b4dd1e5e97b800c6d7d79e437b48 to your computer and use it in GitHub Desktop.

Convert <ingredients> to JSON using the following format:

  • If an ingredient has both weight and volume, extract only the weight:

    ¾ cup (150g) dark brown sugar
    [{"name": "dark brown sugar", "quantity": 150, "unit": "g"}]
    
  • If an ingredient only lists a volume, extract that.

    2 teaspoons ground cinnamon
    ⅓ cup (80ml) neutral oil
    [
      {"name": "ground cinnamon", "quantity": 2, "unit": "teaspoon"},
      {"name": "neutral oil", "quantity": 80, "unit": "ml"}
    ]
    
  • If the unit uses a fraction, convert it to a decimal.

    ⅓ cup sugar
    ½ teaspoon salt
    [
      {"name": "dark brown sugar", "quantity": 0.33, "unit": "cup"},
      {"name": "salt", "quantity": 0.5, "unit": "teaspoon"}
    ]
    
  • Quantities are always numbers

    pinch of kosher salt
    [
      {"name": "kosher salt", "quantity": 1, "unit": "pinch"}
    ]
    
  • Some ingredients don't have a unit.

    2 eggs
    1 lime
    1 apple
    [
      {"name": "egg", "quantity": 2},
      {"name": "lime", "quantity": 1},
      {"name", "apple", "quantity": 1}
    ]
    
  • Ingredient size is its own field

    1 medium orange
    2 large peach
    3 small eggs
    [
      {"name": "orange", "quantity": 1, "size": "medium"},
      {"name": "peach", "quantity": 2, "size": "large"},
      {"name": "egg", "quantity": 3, "size": "small"}
    ]
    
  • Compound ingredients should each get their own record

    150g plus 1½ teaspoons sugar
    [
      {"name": "sugar", "quantity": 150, "unit": "g"},
      {"name": "sugar", "quantity": 1.5, "unit": "teaspoon"}
    ]
    
  • Additional details are extracted into notes

    2 small apples, peeled, cored, and chopped into ¼- to ½-inch pieces (about 2 cups) ½ cup (85g) chopped white chocolate or white chocolate chips

    [
      {"name": "white chocolate", "quantity": 85, "unit": "g", "notes": "chopped or chips"}
      {"name": "apple", "size": "small", "quantity": 2, "notes": "peeled, cored, and chopped into ¼- to ½-inch pieces, about 2 cups"}
    ]
    
  • Temperature is also included in the note

    1 cup boiling water
    1 cup hot milk
    [
      {"name": "water", "quantity": 1, "unit": "cup", "notes": "boiling"},
      {"name": "milk", "quantity": 1, "unit": "cup", "notes": "hot"}
    ]
    
  • A complete ingredient list is always a single array

    1 cup flour
    1 egg
    1 cup sugar plus 3 tablespoons sugar
    [
      {"name": "flour", "quantity", 1, "unit": "cup"},
      {"name": "egg", "quantity": 1},
      {"name": "sugar", "quantity": 1, "unit": "cup"},
      {"name": "sugar", "quantity": 3, "unit": "tablespoon"}
    ]
    

<ingredients> [[ingredients]] <ingredients>

Only provide json in the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment