0
js = {
   "status": "ok",
   "meta": {
      "count": 1
   },
   "data": {
      "542250529": [
         {
            "all": {
               "spotted": 438,
               "battles_on_stunning_vehicles": 0,
               "avg_damage_blocked": 39.4,
               "capture_points": 40,
               "explosion_hits": 0,
               "piercings": 3519,
               "xp": 376586,
               "survived_battles": 136,
               "dropped_capture_points": 382,
               "damage_dealt": 783555,
               "hits_percents": 74,
               "draws": 2,
               "battles": 290,
               "damage_received": 330011,
               "frags": 584,
               "stun_number": 0,
               "direct_hits_received": 1164,
               "stun_assisted_damage": 0,
               "hits": 4320,
               "battle_avg_xp": 1299,
               "wins": 202,
               "losses": 86,
               "piercings_received": 1004,
               "no_damage_direct_hits_received": 103,
               "shots": 5857,
               "explosion_hits_received": 135,
               "tanking_factor": 0.04
            }
         }
      ]
   }
}

Let us name this json "js" as a variable, this variable will be in a for-loop. To understand better what I'm doing here, I'm trying to collect data from a game. This game has hundreds of different tanks, each tank has tank_id with which I can post tank_id to the game server and respond the performance data as "js". for tank_id: json = requests.post(tank_id) etc... and fetch all these values to my database as shown in the screenshot. database

my python code for it:

def api_get():
 for property in js['data']['542250529']['all']:
   spotted = property['spotted']
   battles_on_stunning_vehicles = property['battles_on_stunning_vehicles']
   # etc
   # ...
   insert_to_db(spotted, battles_on_stunning_vehicles, etc....)

the exception is:

for property in js['data']['542250529']['all']:
 TypeError: list indices must be integers or slices, not str

and when:

print(js['data']['542250529'])

i get the rest of the js as a string, and i can't iterate... can't be used a valid json string, also what's inside js['data']['542250529'] is a list containing only the item 'all'..., any help would be appreciated

1
  • 1
    542250529 contains a list: js['data']['542250529'][0]['all'] Commented Nov 22, 2021 at 12:38

2 Answers 2

2

You just missed [0] to get the first item in a list:

def api_get():
   for property in js['data']['542250529'][0]['all']:
       spotted = property['spotted']
# ...

Look carefully at the data structure in the source JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

@Master Schifou this will also work.
I didn't sleep since yesterday trying to find how stupid I am lol, first item in a list get 0 as an index, i appreciate your efforts big boss <3
0

There is a list containing the dictionary with a key of all. So you need to use js['data']['542250529'][0]['all'] not js['data']['542250529']['all']. Then you can use .items() to get the key-value pairs.

See below.

js = {
   "status": "ok",
   "meta": {
      "count": 1
   },
   "data": {
      "542250529": [
         {
            "all": {
               "spotted": 438,
               "battles_on_stunning_vehicles": 0,
               "avg_damage_blocked": 39.4,
               "capture_points": 40,
               "explosion_hits": 0,
               "piercings": 3519,
               "xp": 376586,
               "survived_battles": 136,
               "dropped_capture_points": 382,
               "damage_dealt": 783555,
               "hits_percents": 74,
               "draws": 2,
               "battles": 290,
               "damage_received": 330011,
               "frags": 584,
               "stun_number": 0,
               "direct_hits_received": 1164,
               "stun_assisted_damage": 0,
               "hits": 4320,
               "battle_avg_xp": 1299,
               "wins": 202,
               "losses": 86,
               "piercings_received": 1004,
               "no_damage_direct_hits_received": 103,
               "shots": 5857,
               "explosion_hits_received": 135,
               "tanking_factor": 0.04
            }
         }
      ]
   }
}

for key, val in js['data']['542250529'][0]['all'].items():
    print("key:", key, " val:", val)

#Or this way

for key in js['data']['542250529'][0]['all']:
    print("key:", key, " val:", js['data']['542250529'][0]['all'][key])

1 Comment

I just found out a solution to make a new for-loop, without knowing how it worked. but thanks to you guys i am doing it the easy way... <3

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.