• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Asset - Scripts JSON Struct: Use structs and arrays to work with JSON

FrostyCat

Redemption Seeker
JSON Struct
Bringing out the true potential of JSON parsing in GMS 2.3+

Overview

This library allows encoding and decoding JSON as structs, arrays and other first-order data types as of GMS 2.3. In addition to standard encoding and decoding functions, it also contains utilities for encryption, pretty-printing, and saving/loading JSON files. With its concise syntax and comprehensively tested code base, JSON Struct will change the way you think about JSON handling in GML.

Downloads / Links
YoYo Marketplace: Link
GitHub: Link | Repository | Wiki

Comparison

Post-2020 GML with JSON Struct:

GML:
json_data = {
  data: [
    {
      name: "Alice"
      hp: 5
    },
    {
      name: "Bob",
      HP: 6
    }
  ]
));
json = jsons_encode(json_data);
Pre-2020 stock GML
GML:
var json_data = ds_map_create();
var sublist = ds_list_create();
ds_map_add_list(json_data, "data", sublist);
ds_list_add(sublist, ds_map_create());
ds_map_add(sublist[| 0], "name", "Alice");
ds_map_add(sublist[| 0], "HP", 5);
ds_list_mark_as_map(sublist, 0);
ds_list_add(sublist, ds_map_create());
ds_map_add(sublist[| 1], "name", "Bob");
ds_map_add(sublist[| 1], "HP", 6);
ds_list_mark_as_map(sublist, 1);

var json = json_encode(json_data);
ds_map_destroy(json_data);

Feedback Welcome!
If you have any suggestions or bug reports, please open an issue or contribute on GitHub.
 
Last edited:

FrostyCat

Redemption Seeker
Update: JSON Struct v1.1.0 now supports pretty-print encoding and saving!

Encoding:
GML:
var jsonStr = jsons_encode_formatted([
    { name: "Alice", hp: 4 },
    { name: "Bob", hp: 5 },
    { name: "Caitlyn", hp: 6 },
]);
show_message(jsonStr);
Saving:
GML:
jsons_save_formatted(working_directory + "characters.json", [
    { name: "Alice", hp: 4 },
    { name: "Bob", hp: 5 },
    { name: "Caitlyn", hp: 6 },
]);
 
Top