[SOLVED] Simplest way to get nested ds_map from JSON?

I'm using JSON files to store data for my game. The JSON file is structured like this:
Code:
{
  "Player1": {
    "name": "Julia",
    "defense": {
      "physical": 0,
      "elemental": 2
    }
  },

  "Player2": {
    "name": "Chris",
    "defense": {
      "physical": 3,
      "elemental": 1
    }
  }
}
Retrieving values like "name" is simple, but finding defense values requires going through nested maps. This is my current solution, but it feels a bit unwieldy:
Code:
map_main = json_decode(json_str);
map_player = map_main[? "Player1"];
map_defense = map_player[? "defense"];
player_defense[defenseType.phys] = map_defense[? "physical"];
Is the way I'm going about it the intended way, or is there a better way to do this that has completely flown over my head?
 

FrostyCat

Redemption Seeker
The ability to chain accessors is still on the roadmap, but for now, the closest alternative is this helper script:
Code:
///json_get(jsondata, ...)
{
var gotten = argument[0];
if (argument_count >= 2 && is_real(argument[1])) {
  gotten = gotten[? "default"];
}
for (var i = 1; i < argument_count; i++) {
  var k = argument[i];
  if (is_real(k)) {
    gotten = gotten[| k];
  } else if (is_string(k)) {
    gotten = gotten[? k];
  } else {
    show_error("Invalid argument " + string(i+1) + " to json_get().", true);
  }
}
return gotten;
}
In your case:
Code:
map_main = json_decode(json_str);
player_defense[defenseType.phys] = json_get(map_main, "Player1", "defense", "physical");
 
The ability to chain accessors is still on the roadmap, but for now, the closest alternative is this helper script:
[snip]
In your case:
Code:
map_main = json_decode(json_str);
player_defense[defenseType.phys] = json_get(map_main, "Player1", "defense", "physical");
Ah, that works perfectly! My project is still in GMS1 for the time being, so that script is my best option. Thank you very much.
 
Top