STEM

How to parse json data from a restaurant

In this blog post, I’ll show you a real-life example of a restaurant menu encoded as a JSON file and how you can parse in your app, and subsequently display it in your custom UI as desired.

Suppose you have a menu of items with details encoded in JSON format that looks like this:

This format is human-readable as well as machine parsable. Essentially, it’s an array of dictionary if you will. There are keys and values pairs, and they can repeat as many times as needed without the parsing app knowing ahead of time how many items are there, or how many elements are there in the array. That’s where the power of parsing of JSON is of utmost importance.

First, we need to investigate the format of the file. JSON is very flexible but it also means there’s no universal way to encode the information. Every JSON file is different requiring understanding of the format first before writing the code to parse it.

Gleaning this file, we see there’s a name of the restaurant (EatPals), type of cuisine (Indian), item number, item name, price, description, and spice level. And that is in an array called ‘menu_items’ which need to be parsed as many times as it has elements (the elements can be added or removed at any time!).

Then we also have information encoded there about its address, phone, email, and hours along with its website. Great! So, if we were to create an app (fancy or not) for potential customers to view and order items from that joint, we must first know how to parse this jSON data either from the Cloud or from a local server into its atomic pieces.

Here’s the code to parse it correctly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json

file_to_open = "JSONdatafile.json" # replace with actual name and path

with open(file_to_open, 'r') as f:
    data = json.load(f)

# Print without formatting/parsing of json data:
print(data)

menu = data['Menu']
cuisine = menu['cuisine']
name = menu['name']
address = menu['address']
contact_phone = menu['contact_phone']
hours = menu['hours']
menu_items = menu['menu_items']

# Display the information
print()
print(f'Name: {name}')
print(f'Cuisine: {cuisine}')
print(f'Address: {address}')
print(f'Contact Phone: {contact_phone}')
print(f'Hours: {hours}')
print('Menu Items:')
for item in menu_items:
    item_num = item['Itemnum']
    name = item['Name']
    price = item['Price']
    desc = item['Desc']
    spice_level = item['Spicelevel']
    print(f'  {item_num}: {name} - {price} - {desc} - Spice Level: {spice_level}')

###

When we run this script, we get an output in the shell as below.

It may not look like much but the hardest part of JSON parsing is 100% done. Now it’s a matter of how you want to display this information in your fancy app UI 🙂

This is a hypothetical restaurant and menu designed for educational purposes only.

Practice by creating more JSON files of your own, they do not have to be in this format at all, and try parsing it with your own code accordingly. This will give you much more practical knowhow, and I hope this was a good start and helpful. Happy parsing!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
+