Smoothies Ingredients Data Set

For this programming challenge, we are going to practise reading and extracting data from a JSON file. We will be using a JSON file storing information about the ingredients and recipes for
20 different smoothies.

JavaScript Object Notation (JSON)

JavaScript Object Notation (JSON) is a lightweight, text-based format for storing and exchanging data that is human and machine-readable. It is a standard format that is often used to transfer/retrieve data from a server using an API. It is used in a wide range of web and mobile applications that rely on accessing/exchanging live data.

A JSON file is like a dictionary data structure. It is made up of two primary parts: keys and values. A key/value pair follows a specific syntax:

  • Key: Always a string enclosed in quotation marks
  • Value: Can be a string, number, Boolean expression, array/list, or object

Complex data structure can be created using JSON by combining {dictionaries} and [arrays/lists].

The smoothies data set: JSON vs. XML format

JSON Data SetXML Data Set
Here is an extract of the smoothies.json file showing the data for the first three smoothies.

Within this file, the main key is “smoothies”. Its value is an array of 20 different smoothies. Each smoothie is stored as a dictionary with 3 keys:

  • Name
  • Ingredients: a list of ingredients
  • Recipe
{
  "Smoothies": [
    {
      "Name": "Strawberry Banana",
      "Ingredients": ["Strawberries", "Banana", "Greek Yogurt", "Honey", "Almond Milk"],
      "Recipe": "Blend 1 cup of strawberries, 1 banana, ½ cup of Greek yogurt, 1 tablespoon of honey, and 1 cup of almond milk. Blend until smooth."
    },
    {
      "Name": "Green Detox",
      "Ingredients": ["Spinach", "Kale", "Green Apple", "Cucumber", "Lemon Juice", "Coconut Water"],
      "Recipe": "Combine 1 cup of spinach, 1 cup of kale, 1 green apple (cored and chopped), ½ cucumber, 1 tablespoon of lemon juice, and 1 cup of coconut water. Blend until smooth."
    },
    {
      "Name": "Mango Pineapple",
      "Ingredients": ["Mango", "Pineapple", "Orange Juice", "Coconut Milk", "Chia Seeds"],
      "Recipe": "Blend 1 cup of mango, 1 cup of pineapple, 1 cup of orange juice, ½ cup of coconut milk, and 1 tablespoon of chia seeds. Blend until smooth and creamy."
    }
  ]
}
An alternative approach to store data is to use an XML file. Here is the same data for the first three smoothies, stored as an XML file.

<Smoothies>
    <Smoothie>
        <Name>Strawberry Banana</Name>
        <Ingredients>
            <Ingredient>Strawberries</Ingredient>
            <Ingredient>Banana</Ingredient>
            <Ingredient>Greek Yogurt</Ingredient>
            <Ingredient>Honey</Ingredient>
            <Ingredient>Almond Milk</Ingredient>
        </Ingredients>
        <Recipe>Blend 1 cup of strawberries, 1 banana, ½ cup of Greek yogurt, 1 tablespoon of honey, and 1 cup of almond milk. Blend until smooth.</Recipe>
    </Smoothie>
    <Smoothie>
        <Name>Green Detox</Name>
        <Ingredients>
            <Ingredient>Spinach</Ingredient>
            <Ingredient>Kale</Ingredient>
            <Ingredient>Green Apple</Ingredient>
            <Ingredient>Cucumber</Ingredient>
            <Ingredient>Lemon Juice</Ingredient>
            <Ingredient>Coconut Water</Ingredient>
        </Ingredients>
        <Recipe>Combine 1 cup of spinach, 1 cup of kale, 1 green apple (cored and chopped), ½ cucumber, 1 tablespoon of lemon juice, and 1 cup of coconut water. Blend until smooth.</Recipe>
    </Smoothie>
    <Smoothie>
        <Name>Mango Pineapple</Name>
        <Ingredients>
            <Ingredient>Mango</Ingredient>
            <Ingredient>Pineapple</Ingredient>
            <Ingredient>Orange Juice</Ingredient>
            <Ingredient>Coconut Milk</Ingredient>
            <Ingredient>Chia Seeds</Ingredient>
        </Ingredients>
        <Recipe>Blend 1 cup of mango, 1 cup of pineapple, 1 cup of orange juice, ½ cup of coconut milk, and 1 tablespoon of chia seeds. Blend until smooth and creamy.</Recipe>
    </Smoothie>
</Smoothies>

Python Code

To be able to read and extract data from our JSON file using Python, we will use the json library. Here is an example of how to use Python code to load the JSON data from the smoothies.json file. We can then perform a basic linear search to retrieve all the smoothies with “Mango” listed in their list of ingredients.

import json
 
# load JSON data from file
file = open('smoothies.json','r')
data = json.load(file)
file.close()

# Perform a linear search using the JSON data
smoothies = data["Smoothies"]
for smoothie in smoothies:
   if "Mango" in smoothie["Ingredients"]:
      print(smoothie["Name"])
      print(smoothie["Recipe"])
      print("--------------------")

You can test and edit this code below:

Your Task:

Your task consists of creating a program that lets the end-user do the following:

     Enter a list of ingredients they would like to have in their smoothie. e.g. Papaya, Pineapple and Mango
     Enter a list of ingredients they do not want to have in their smoothie. e.g. Peanut Butter and Cocoa Powder.
     Retrieve a list of all smoothies matching these criteria.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 5

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: ,