0
votes
{  
   "sessionid":"1",
   "game_clock_display":"1",
   "game_clock":1,
   "game_status":"score",
   "possession":[  
      1,
      1
   ],
   "teams":[  
      {  
         "players":[  
            {  
               "name":"NAME1",
               "playerid":1,
               "position":[  
                  64,
                  34,
                  45
               ],
               "stats":{  
                  "possession_time":21.882006,
                  "points":3,
                  "saves":1,
                  "goals":0,
                  "stuns":4,
                  "passes":0,
                  "catches":0,
                  "steals":1,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":2
               },
               "userid":xxx,
               "possession":false
            },
            {  
               "name":"NAME2",
               "playerid":3,
               "position":[  
                  -1.251,
                  2.8280001,
                  -24.380001
               ],
               "stats":{  
                  "possession_time":25.58037,
                  "points":2,
                  "saves":0,
                  "goals":0,
                  "stuns":4,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":1
               },
               "userid":1583793368337226,
               "possession":false
            },
            {  
               "name":"NAME3",
               "playerid":5,
               "position":[  
                  0.89900005,
                  3.0540001,
                  -39.322002
               ],
               "stats":{  
                  "possession_time":28.325825,
                  "points":0,
                  "saves":1,
                  "goals":0,
                  "stuns":2,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":2
               },
               "userid":1462448290539019,
               "possession":false
            }
         ],
         "team":"BLUE TEAM",
         "possession":false,
         "stats":{  
            "points":5,
            "possession_time":75.7882,
            "interceptions":0,
            "blocks":0,
            "steals":1,
            "catches":0,
            "passes":0,
            "saves":2,
            "goals":0,
            "stuns":10,
            "assists":0,
            "shots_taken":5
         }
      },
      {  
         "players":[  
            {  
               "name":"NAME4",
               "playerid":0,
               "position":[  
                  -0.058000002,
                  3.7800002,
                  3.9600003
               ],
               "stats":{  
                  "possession_time":15.404906,
                  "points":0,
                  "saves":2,
                  "goals":0,
                  "stuns":5,
                  "passes":0,
                  "catches":0,
                  "steals":1,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":1
               },
               "userid":2022757824404800,
               "possession":false
            },
            {  
               "name":"NAME5",
               "playerid":1,
               "position":[  
                  12.033001,
                  1.0680001,
                  -31.469002
               ],
               "stats":{  
                  "possession_time":29.619047,
                  "points":3,
                  "saves":0,
                  "goals":0,
                  "stuns":5,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":0,
                  "shots_taken":2
               },
               "userid":1504614686325452,
               "possession":true
            },
            {  
               "name":"NAME6",
               "playerid":4,
               "position":[  
                  0.78100002,
                  1.9790001,
                  -17.815001
               ],
               "stats":{  
                  "possession_time":15.020118,
                  "points":2,
                  "saves":0,
                  "goals":0,
                  "stuns":2,
                  "passes":0,
                  "catches":0,
                  "steals":0,
                  "blocks":0,
                  "interceptions":0,
                  "assists":1,
                  "shots_taken":0
               },
               "userid":2207645752583213,
               "possession":false
            }
         ],
         "team":"ORANGE TEAM",
         "possession":true,
         "stats":{  
            "points":5,
            "possession_time":60.044071,
            "interceptions":0,
            "blocks":0,
            "steals":1,
            "catches":0,
            "passes":0,
            "saves":2,
            "goals":0,
            "stuns":12,
            "assists":1,
            "shots_taken":3
         }
      }
   ]
}

My JSON file, I would like to print the value of "name" with "position" and team, ie blue or orange team.

So I want my final output in python to be

Name: NAME1, postion: X,Y,Z Team: Blue

Name: NAME2, postion: X,Y,Z Team: Blue

Name: NAME3, postion: X,Y,Z Team: Blue

Name: NAME4, postion: X,Y,Z Team: Orange

Name: NAME5, postion: X,Y,Z Team: Orange

Name: NAME6, postion: X,Y,Z Team: Orange

for example.

How will I go about to do this? Quite new to python

2
What code do you have so far? How are you reading in the file? Provide a piece of code showing your current progress.ArdentLearner

2 Answers

1
votes

You could look into the json module:

import json

j = json.load(open("json-t.txt"))

for team in j['teams']:
    for p in team['players']:
        print(f"Name: {p['name']}, position: {p['position']}, team: {team['team']}") 

Note: in your example json you left in "userid": xxx, make sure this is fixed.

0
votes

Using the json module you can load the file into a standard Python dict

import json

data = None

with open(filename, 'r') as f:
    data = json.load(f)

teams = data['teams']

now you can iterate teams to extract the data you need