1

I have this JSON file where the amount of id's sometimes changes (more id's will be added):

{
    "maps": [
        {
            "id": "blabla1",
            "iscategorical": "0"
        },
        {
            "id": "blabla2",
            "iscategorical": "0"
        },
        {
            "id": "blabla3",
            "iscategorical": "0"
        },
        {
            "id": "blabla4",
            "iscategorical": "0"
        }
    ]
}

I have this python code that has to print all the values of ids:

import json

data = json.load(open('data.json'))
variable1 = data["maps"][0]["id"]
print(variable1)
variable2 = data["maps"][1]["id"]
print(variable2)
variable3 = data["maps"][2]["id"]
print(variable3)
variable4 = data["maps"][3]["id"]
print(variable4)

I have to use variables, because i want to show the values in a dropdown menu. Is it possible to save the values of the id's in a more efficient way? How do you know the max amount of id's of this json file (in de example 4)?

Mvz
  • 507
  • 3
  • 11
  • 29

2 Answers2

2

You can get the number of id (which is the number of elements) by checking the length of data['maps']:

number_of_ids = len(data['maps'])

A clean way to get all the id values is storing them in a list.

You can achieve this in a pythonic way like this:

list_of_ids = [map['id'] for map in data['maps']]

Using this approach you don't even need to store the number of elements in the original json, because you iterate through all of them using a foreach approach, essentially.

If the pythonic approach troubles you, you can achieve the same thing with a classic foreach approach doing so:

list_of_ids = []
for map in data['maps']:
    list_of_ids.append(map['id'])

Or you can do with a classic for loop, and here is where you really need the length:

number_of_ids = len(data['maps'])
list_of_ids = []
for i in range(0,number_of_ids):
    list_of_ids.append(data['maps'][i]['id'])

This last is the classic way, but I suggest you to take the others approaches in order to leverage the advantages python offers to you!

You can find more on this stuff here!

Happy coding!

magicleon94
  • 4,887
  • 2
  • 24
  • 53
1

data['maps'] is a simple list, so you can iterate over it as such:

for map in data['maps']:
    print(map['id'])

To store them in a variable, you'll need to output them to a list. Storing them each in a separate variable is not a good idea, because like you said, you don't have a way to know how many there are.

ids = []
for map in data['maps']:
    ids.append(map['id'])
Adam Barnes
  • 2,922
  • 21
  • 27
  • 1
    The OP also asked how many IDs were in the list. – Matt Morgan Feb 02 '18 at 11:22
  • Because of the dropdown menu I want to make, I have to store them in variables. Here I'm stuck. This example is a good way to start – Mvz Feb 02 '18 at 11:22
  • How are you creating the "dropdown"? I can't help more without seeing your code for that, or at least what framework. Please update your question. – Adam Barnes Feb 02 '18 at 11:28
  • I used a list for the dropdown. Thank you for changing the post with a list! OptionMenu(root, self.dropBoard, *ids).grid(row=0, column=0) – Mvz Feb 02 '18 at 11:33