json_stringify

With this function you can convert single or nested structs and arrays into a valid JSON string. You supply the initial value to use (an array index or a struct reference) and then the function will "stringify" it, converting it into a JSON string (converting GameMaker arrays into JSON arrays, and GameMaker structs into JSON objects).

When using this function there are some important things to note:

 

 

 

Syntax:

json_stringify(val)

Argument Type Description
val Struct or Array The reference value for a struct or array to convert into a JSON string

 

Returns:

String

 

Example:

var _contents =
{
    version : "1.0.0",
    data:
    {
        coins : 4,
        mana : 15,
        playername : "Gurpreet",
        items :
        [
            ITEM.SWORD,
            ITEM.BOW,
            ITEM.GUITAR
        ]
    }
};

var _json_string = json_stringify(_contents);

The above code will convert the _contents struct into a JSON string and store the string in a variable. The returned string would look like this:

{ "data": { "items": [ 0.0, 1.0, 2.0 ], "coins": 4.0, "mana": 15.0, "playername": "Gurpreet" }, "version": "1.0.0" }