Having trouble getting data from JSON dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wwave72

Hi, I’m having problems getting specific data from my JSON file, I already can get most of the items in the dictionary but the ones that have a int as reference don’t work correctly.

This is a portion of the JSON file

"hero0": {
"name": "Placeholder",
"inparty": true,
"level": 16,
"currentHP": 534,
"job1": 9,
"job2": 0,
"equipment": {
  "righth": 1,
  "lefth": 0,
  "head": 0,
  "armor": 0,
  "ring1": 0,
  "ring2": 0,
  "amulet": 0
},
"joblevel": {
  "0": 0,
  "1": 4,
  "2": 0,
  "3": 0,
  "4": 0,
  "5": 0,
  "6": 0,
  "7": 0,
  "8": 0,
  "9": 0
}

With the following code i can extract the ID of each equipment slot

var hero0EqHead
hero0JobData = ImportData.party_data["hero0"]["equipment"].head

Party data is my variable wih the JSON data but when I try the same as above but with a number at the end I get the following error.

hero0JobData = ImportData.party_data["hero0"]["joblevel"].1

error(117,61): Expected end of statement after expression, got Constant instead

I wanted to use integers to make a for loop to get all data from all “jobs” with a code that (if worked) looked like this

hero0JobData = ImportData.party_data["hero0"]["joblevel"].n

With “n” being the loop variable.

So, wrapping up. I want to get specific JSON data with a variable that correspond the ID of the item that I’m looking for.

I have tried reading the documentation but haven’t found anything useful, any help would be very appreciated

:bust_in_silhouette: Reply From: ponponyaya

Those keys have type String in your dictionary, so try this way:

ImportData.party_data["hero0"]["joblevel"]["1"]

or

ImportData.party_data["hero0"]["joblevel"].get("1")

In loop with variable n, you can use it like this:

ImportData.party_data["hero0"]["joblevel"][str(n)]

or

ImportData.party_data["hero0"]["joblevel"].get(str(n))