0

I'm working on a Python Pygame project were I have to work with JSON files. I'm trying to read a JSON file, but I just can't get it to print what I want to know.

This is the JSON file:

{
  "pokemons": {
    "5": {
      "name": "Snivy",
      "type": "Grass",
      "hp": 45,
      "attack": 45,
      "defence": 55,
      "speed": 63,
      "moves": [
        "Tackle",
        "Leer",
        "null",
        "null"
      ],
      "level": 4,
      "xp": 54
    },
    "2": {
      "name": "Tepig",
      "type": "Fire",
      "hp": 65,
      "attack": 63,
      "defence": 45,
      "speed": 45,
      "moves": [
        "Tackle",
        "Tail Whip",
        "Ember",
        "null"
      ],
      "level": 7,
      "xp": 11
    }
  }
} 

I'm trying to read the "name", "type", etc from the different "ID's" aka "5" and "2", but I can only make it print "5" and "2" from the "pokemons" array

with open("data.json", "r") as f:
    data = json.load(f)
    for i in data["pokemons"]:
        print(i)
1
  • Iterate a dictionary as for k,v in data['pokemonds'].items(): print (k, v) Commented Apr 26, 2020 at 22:14

2 Answers 2

2

You've titled this "json read from array inside of array python", but you don't have JSON arrays (translated into Python lists) here - you have JSON objects (translated into Python dicts).

for i in data["pokemons"]:

data["pokemons"] is a dict, so iterating over it like this gives you the keys - "5" and "2". You can use those to index into the data:

data["pokemons"][i]

That gives you one of the objects (dicts) representing an individual Pokemon, from which you can access the name:

data["pokemons"][i]["name"]

Better yet, you can loop over the values of data["pokemons"] directly, instead of the keys:

for pokemon in data["pokemons"].values():
    name = pokemon["name"]

Or you can get both at once using .items(), for example:

for pid, pokemon in data["pokemons"].items():
    # use string formatting to display the pid and matching name together.
    print(f"pokemon number {pid} has name {pokemon['name']}")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, im sorry for the wrong title, will change that. I have not worked that much with JSON files before so im realy happy for the help, thanks again!
It's not really a question of "working with JSON files" - once you have made the .load call, it's ordinary Python data. But sometimes this is someone's first time dealing with deeply nested structures, I suppose.
1

My Solution

data = '{"pokemons": {"5": {"name": "Snivy","type": "Grass","hp": 45,"attack": 45,"defence": 55,"speed": 63,"moves": ["Tackle","Leer","null","null"],"level": 4,"xp": 54},"2": {"name": "Tepig","type": "Fire","hp": 65,"attack": 63,"defence": 45,"speed": 45,"moves": ["Tackle","Tail Whip","Ember","null"],"level": 7,"xp": 11}}}}'
datadict = json.loads(data)
dataOfId = datadict['pokemons']
for i in dataOfId:
      print(dataOfId[i]["name"])
      print(dataOfId[i]["type"])

Comments

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.