How do i grab items from json file

So im making a game and I want to save the stats of items in a json file, so how would i get the individual stats of each item like getting the hp and defense from different items
Here’s the json contents:

{
  "Armors": {
    "hell armor": {
      "hp": 4,
      "defense": 9
    }
    "leather armor": {
      "hp": 5,
      "defense": 6
    }
  }
  "shields": {
    "leather shield": {
      "hp": 4,
      "defense": 5
    }
  }
}

Use the json standard library

import json

with open("stats.json") as file:
    stats = json.load(file)

leather_armor = stats["leather armor"]
leather_armor["hp"]

So i tried doing this to see what the output would be and this happened

with open("survival_game_data.json") as file:
    stats = json.load(file)

leather_armor = stats["leather armor"]
leather_armor["hp"]

And here’s the error:

Your JSON file is wrong, it is missing commas

{
  "Armors": {
    "hell armor": {
      "hp": 4,
      "defense": 9
    },
    "leather armor": {
      "hp": 5,
      "defense": 6
    }
  },
  "shields": {
    "leather shield": {
      "hp": 4,
      "defense": 5
    }
  }
}

It says there is a key error in leather armor and what does this part do

leather_armor["hp"]

Oops

import json

with open("stats.json") as file:
    stats = json.load(file)

leather_armor = stats["Armors"]["leather armor"]

That was to show you how you can access a specific property, you don’t need that

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.