44 | Product Search |
Pulling data from a file into a complex data structure makes parsing much simpler. Many programming languages support the JSON format, a popular way of representing data.
Create a program that takes a product name as input and retrieves the current price and quantity for that product. The product data is in a data file in the JSON format and looks like this:
| { |
| "products" : [ |
| {"name": "Widget", "price": 25.00, "quantity": 5 }, |
| {"name": "Thing", "price": 15.00, "quantity": 5 }, |
| {"name": "Doodad", "price": 5.00, "quantity": 10 } |
| ] |
| } |
Print out the product name, price, and quantity if the product is found. If no product matches the search, state that no product was found and start over.
Example Output
| What is the product ... |
Get Exercises for Programmers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.