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 !

4
  • Why not use j = {"TEAM": teams_}? Commented Jul 8, 2021 at 12:56
  • because i don't wan't the numbers before Commented Jul 8, 2021 at 12:58
  • with my solution if you 100 teams you will have to put teams_[0],teams[1],...teams[99] Commented Jul 8, 2021 at 13:02
  • Does this answer your question? How can I get list of values from dict? Commented Jul 8, 2021 at 13:03

1 Answer 1

1

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

j = {"TEAM" : list(teams_.values())} 
Sign up to request clarification or add additional context in comments.

1 Comment

cool. be sure to accept the solution please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.