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 in to JSON objects.

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

 

 

 

Syntax:

json_stringify(val)

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

 

Returns:

string

 

Example:

var _contents = {
    version : "1",
    data: {
        webSocket : 1,
        ipv4Address : "95.39.220.218",
        ipv6Address : "0000:0000:0000:0000:0000:ffff:5f27:dcda",
        header : "You are receiving a packet",
        body : "This packet is empty and only for testing",
    },    
};
packet = json_stringify(map);

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

{ "version": "1", "myObj": { "header": "You are receiving a packet", "webSocket": 1, "body": "A packet of cheese and onion crisps", "ipv4Address": "95.39.220.218", "ipv6Address": "0000:0000:0000:0000:0000:ffff:5f27:dcda" } }