• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ Using json parse returns the values in different orders from time to time

Aaron Craig

I'm working on a card game and have created a json file to import all the card details. The problem is the parsed json changes the order it comes in, seemingly at random. It seems like if I add any extra code into my project, anywhere, it may change it. It only seems to alternate between two values, not totally change, but I can't understand why.
Here's the parsing code:
//Read in the cards from the file in the dataFiles folder of project
if (file_exists(working_directory + "creatures.json")) {
    json = "";
    //Open the file
    file = file_text_open_read(working_directory + "creatures.json");
    //Read the entire file as one string
    while(file_text_eof(file) == false) {
        json += file_text_readln(file);
    //Save the json in the global variable
    global.MasterCardList = json_parse(json);
Then I take those values and save them:

//Assign globals to the struct names
var array = variable_struct_get_names(global.MasterCardList[0]);

global.CardName = array[1]
global.CardType = array[4]
global.CardAttack = array[5]
global.CardDefense = array[6]
global.CardSpecies = array[7]
global.CardElement = array[8]
global.CardLevel = array[0]
global.CardAbilityText = array[2]
global.CardFlavorText = array[3]
I know I could build a sorting function for the returned values, but I thought this might be a language issue or bug. Has anyone else experienced this?



Your decode result shows a struct at global.MasterCardList[0], not an array. Here's an example of what you should have done:
var card = global.MasterCardList[0];

global.CardName = card.Card_Name;
Starting with GMS 2.3.1, you could also do it like card[$ "Card_Name"].