GameMaker [SOLVED] Best method for loading JSON help

W

William Roberts

Guest
I am looking for the best way to handle a JSON file with over 200k objects. Each object has 4 fields. I've worked with loading text files before, but not CSV or JSON.

Right now, the data is CSV and JSON but wasn't sure the best method for getting it into GMS. I was thinking that pulling the data into a ds_map would be the best method, but I don't know.

I will only ever need to access one row/object at a time.

Thanks and any help or insight is greatly appreciated.

Very respectfully,
Will
 

FrostyCat

Redemption Seeker
GMS 2 comes with a load_csv() function for CSV, and you can combine text file functions with json_decode() for JSON (example). Once it's loaded as a data structure (grid for CSV, combination of maps and/or lists for JSON), it's just a matter of using accessors and/or data structure functions to get at what you want.
 
W

William Roberts

Guest
Thank you for the references. I guess I'm not seeing how the JSON decoding process works. Let's say I have a list of names with associated data, each JSON object has four pieces;

Name, hp, mp, description

I read the JSON text file into a variable, then I run the json_decode on that variable, how do I extract the data.

I'll only ever need to get one name from the list at a time, and it can be a random name too, but I need all of the associated data that comes with it.

Thanks again,
Will
 

chamaeleon

Member
Thank you for the references. I guess I'm not seeing how the JSON decoding process works. Let's say I have a list of names with associated data, each JSON object has four pieces;

Name, hp, mp, description

I read the JSON text file into a variable, then I run the json_decode on that variable, how do I extract the data.

I'll only ever need to get one name from the list at a time, and it can be a random name too, but I need all of the associated data that comes with it.

Thanks again,
Will
If it is valid Json you will end up with a ds_map so read up on data structures in the manual to find out how to extract information from it.
 

FrostyCat

Redemption Seeker
Traversing down a JSON structure is not hard, you just need to understand the structure of your data before you attempt to work with it.

Assuming that your data is in this list-of-maps form:
Code:
[
  {
    "name": "Alice",
    "hp": 100,
    "mp": 50,
    "description": "Description of Alice"
  },
  {
    "name": "Bob",
    "hp": 75,
    "mp": 75,
    "description": "Description of Bob"
  }
]
Then the information in this post should be sufficient. For example, the addressing sequence for "Alice" is "default" > 0 > "name".
Code:
target = json_data[? "default"];
target = target[| 0];
target = target[? "name"];
// target now holds "Alice"
 
W

William Roberts

Guest
FrostyCat,

Thank you very much, I am now working with JSON and calling records I need from it. This was perfect advice!

Will
 
Top