0

I'm here because i try to generate a proper JSON file with python and actually i'm stuck.

My problem is the next one :

I'm getting my data from a google sheet and i store them in a python dictionary called "teams_" as you can see in the next part of my code.

teams_ = {}
nbTeam = 0

for row in values:
    teams_[nbTeam] = {"NAME":"{}".format(row[1]),
                          "PLAYERS" : [
                              "{}".format(row[2]),
                              "{}".format(row[3])
                          ]
                     }
    nbTeam = nbTeam +1
print(teams_)

the result is the next

{
    0: {'NAME': 'A', 'PLAYERS': ['AA1', 'AA2']},
    1: {'NAME': 'B', 'PLAYERS': ['BB1', 'BB2']}, 
    2: {'NAME': 'C', 'PLAYERS': ['CC1', 'CC2']}, 
    3: {'NAME': 'D', 'PLAYERS': ['DD1', 'DD2']}, 
    4: {'NAME': 'E', 'PLAYERS': ['EE1', 'EE2']}, 
    5: {'NAME': 'F', 'PLAYERS': ['FF1', 'FF2']}, 
    6: {'NAME': 'G', 'PLAYERS': ['GG1', 'GG2']}, 
    7: {'NAME': 'H', 'PLAYERS': ['HH1', 'HH2']}
}

now if i want to put it in a JSON file i tried at first this solution

j = {"TEAM" : [ teams_ ]} 

datas.write(json.dumps(j, indent=4))

and this made something like that :

{
    "TEAM": [
        {
            "0": {
                "NAME": "A",
                "PLAYERS": [
                    "AA1",
                    "AA2"
                ]
            },
            "1": {
                "NAME": "B",
                "PLAYERS": [
                    "BB1",
                    "BB2"
                ]
            },
            "2": {
                "NAME": "C",
                "PLAYERS": [
                    "CC1",
                    "CC2"
                ]
            },
            "3": {
                "NAME": "D",
                "PLAYERS": [
                    "DD1",
                    "DD2"
                ]
            },
            "4": {
                "NAME": "E",
                "PLAYERS": [
                    "EE1",
                    "EE2"
                ]
            },
            "5": {
                "NAME": "F",
                "PLAYERS": [
                    "FF1",
                    "FF2"
                ]
            },
            "6": {
                "NAME": "G",
                "PLAYERS": [
                    "GG1",
                    "GG2"
                ]
            },
            "7": {
                "NAME": "H",
                "PLAYERS": [
                    "HH1",
                    "HH2"
                ]
            }
        }
    ]
}

So i tried by another way :

j = {"TEAM" : [ teams_[0], teams_[1], teams_[2],teams_[3], teams_[4], teams_[5],teams_[6], teams_[7] ]} 

datas.write(json.dumps(j, indent=4))

and this worked fine for the output :

{
    "TEAM": [
        {
            "NAME": "A",
            "PLAYERS": [
                "AA1",
                "AA2"
            ]
        },
        {
            "NAME": "B",
            "PLAYERS": [
                "BB1",
                "BB2"
            ]
        },
        {
            "NAME": "C",
            "PLAYERS": [
                "CC1",
                "CC2"
            ]
        },
        {
            "NAME": "D",
            "PLAYERS": [
                "DD1",
                "DD2"
            ]
        },
        {
            "NAME": "E",
            "PLAYERS": [
                "EE1",
                "EE2"
            ]
        },
        {
            "NAME": "F",
            "PLAYERS": [
                "FF1",
                "FF2"
            ]
        },
        {
            "NAME": "G",
            "PLAYERS": [
                "GG1",
                "GG2"
            ]
        },
        {
            "NAME": "H",
            "PLAYERS": [
                "HH1",
                "HH2"
            ]
        }
    ]
}

But what about if i have 12 or 100 teams ? so i'm looking for a better way to do this and this is the point where i'm stuck. Does anyone know any method to do it "automatically" ?

Thank you for your time !

1 Answers1

1

Use the .values() in your teams_ dictionary, as a list, and that should work then. So try:

j = {"TEAM" : list(teams_.values())} 
chitown88
  • 27,527
  • 4
  • 30
  • 59