0

So I am working on learning Python and have a project I am working on involving keeping track of vending machines. I was given JSON files to get the information I need, but I don't know how to read it in. My teacher said it is formatted to be read in easily with every vending machine slot as a dictionary but I am very unfamiliar with dictionaries and don't understand how to do it. If someone could quickly give me an example of how to read this in I would really appreciate it because I really don't even understand where to start.

Here is a section of the JSON file:

{

    "contents": [
        {
            "row": "A",
            "slots": [
                {
                    "current_stock": 2,
                    "item_name": "Coke",
                    "item_price": 1.75,
                    "last_stock": 3,
                    "slot_number": 1
                },
                {
                    "current_stock": 0,
                    "item_name": "Coke",
                    "item_price": 1.75,
                    "last_stock": 6,
                    "slot_number": 2
                },
                {
                    "current_stock": 1,
                    "item_name": "Coke",
                    "item_price": 1.75,
                    "last_stock": 2,
                    "slot_number": 3
                },
                {
                    "current_stock": 3,
                    "item_name": "Coke",
                    "item_price": 1.75,
                    "last_stock": 3,
                    "slot_number": 4
                },
                {
                    "current_stock": 2,
                    "item_name": "Coke",
                    "item_price": 1.75,
                    "last_stock": 2,
                    "slot_number": 5
                },

                {
                    "current_stock": 0,
                    "item_name": "Coke",
                    "item_price": 1.75,
                    "last_stock": 5,
                    "slot_number": 6
                },
                {
                    "current_stock": 6,
                    "item_name": "Coke Zero",
                    "item_price": 1.75,
                    "last_stock": 8,
                    "slot_number": 7
                },
                {
                    "current_stock": 2,
                    "item_name": "Coke Zero",
                    "item_price": 1.75,
                    "last_stock": 4,
                    "slot_number": 8
                },
                {
                    "current_stock": 4,
                    "item_name": "Coke Zero",
                    "item_price": 1.75,
                    "last_stock": 7,
                    "slot_number": 9
                }
            ]

 },
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    There's a built-in module to parse JSON: https://docs.python.org/3.8/library/json.html – ForceBru Mar 24 '20 at 15:50
  • Note that JSON objects and Python `dict` literals look very similar. It can be convenient, but it can also be a source of confusion. – chepner Mar 24 '20 at 15:54
  • Hi and welcome to stackoverflow! The answer https://stackoverflow.com/a/20199213/1723886 explains how to load json files. What it does is give a dictionary for you. – Alex Telon Mar 24 '20 at 15:55
  • 1
    Does this answer your question? [Reading JSON from a file?](https://stackoverflow.com/questions/20199126/reading-json-from-a-file) – Jongware Mar 24 '20 at 15:57

1 Answers1

0

You want the json library.

To go from json to dictionary

x = json.loads(YourDictionary)

to go from dictionary to json

y = json.dumps(YourDictionary)

here's a link reference: https://www.w3schools.com/python/python_json.asp

mark pedersen
  • 245
  • 1
  • 9