-1

I am getting an error while attempting to read one value from a json. The response from "https://status.mojang.com/check" is a list, which I then attempt to use only one element of. This gives me the Command raised an exception: KeyError: 'data' error.

My code

    url = "https://status.mojang.com/check"
    response = requests.get(url)
    json_data = json.loads(response.text)
    MinecraftNet = json_data[0]
    Accounts = json_data[3]
    Auth = json_data[4]
    Skins = json_data[5]
    Sessions = json_data[7]
    API = json_data[8]


    MinecraftNet = MinecraftNet["data"]["minecraft.net"]
    Accounts = Accounts["data"]["account.mojang.com"]
    Auth = Auth["data"]["auth.mojang.com"]
    Skins = Skins["data"]["skins.minecraft.net"]
    Sessions = Sessions["data"]["sessionserver.mojang.com"]
    API = API["data"]["{'api.mojang.com':'"]
    print(MinecraftNet, Accounts, Auth, Skins, Sessions, API)
Jacques Amsel
  • 1,061
  • 2
  • 14
  • 30
  • 2
    Looks like the object MineCraftNet hasn't a field call "data". Could you print the json sgring received from the get request? – S-Wing Jan 11 '18 at 08:52
  • 1
    If you look at the json data you'll notice that it doesn't have a "data" key, and that "minecraft.net" is at the root level of the data. Try `MinecraftNet["minecraft.net"]`. – Aran-Fey Jan 11 '18 at 08:57
  • There appear to be multiple typos in the code, or at least the code doesn't match the data structure. It's possible that this should be a duplicate of either https://stackoverflow.com/questions/12788217/how-to-extract-a-single-value-from-json-response or even https://stackoverflow.com/questions/48193502/how-can-i-access-the-nested-data-in-this-complex-json-which-includes-another-js (that `"{'api.mojang.com':'"` key looks suspicious), but there's no way to answer the question properly without an [mre]. – Karl Knechtel Jul 02 '22 at 02:19

1 Answers1

1

After executing

MinecraftNet = json_data[0]

your MinecraftNet object contains

{
    minecraft.net: "green"
}

so the next call

MinecraftNet = MinecraftNet["data"]

is wrong.

Change it to

MinecraftNet = MinecraftNet["minecraft.net"]
Northern Poet
  • 1,955
  • 1
  • 12
  • 17