8 Working with JSON: Save the order

 

In this chapter

  • Erik learns about JSON format and files
  • Erik learns about Python modules
  • Erik creates a list of orders
  • Simon and Erik write a function to save orders in a JSON file

“You said something about Jason, yesterday,” Erik asked Simon. “But you said it’s another Jason, not the one from my math class.”

“Yes, that’s another Jason,” Simon smiled. “It’s JSON, J–S–O–N, the file format we use to store data.”

“Like the files we used to store menus?”

“Yes, similar,” Simon answered. “But this format is very good for storing structured data.”

“What’s that?” Erik asked.

“Sometimes you want to store just a piece of text or an image. They usually don’t have any fixed structure. Text is just text. And an image can be large or small, it can be black and white or color, but it doesn’t have any structure—it’s just a bunch of pixels. This is what we call unstructured data.

“But in your case, each order has a structure. Each order has the customer’s name and all the components of the drink you’re going to prepare—no more, no less. It always has the main drink, the flavor, and the topping. Because of the menu you wrote, the customer should answer all these questions before you can print or save the order. Also, the customer can’t add anything to the order.”

“Like another topping?” Erik asked.

“Yes,” Simon said. “You order is an example of structured data.”

Erik didn’t think that he had created something with such a serious name.

New things you have learned today

Code for this chapter