Skip to main content
The JSON module provides functions to convert between GSC arrays/objects and JSON strings, enabling easy data serialization and deserialization.

Module Import

#include unleashed\json;

Functions

encode()

Encodes a GSC array or object into a JSON string.
arr = [];
arr[arr.size] = "abc";
arr[arr.size] = "123";
arr[arr.size] = "xyz";
str = unleashed\json::encode(arr);
// Returns: ["abc","123","xyz"]
Parameters:
  • obj - Array or object to encode
Returns: JSON string representation
The function automatically detects whether to encode as a JSON array or object based on the keys. Sequential numeric keys starting from 0 produce arrays, while string keys or non-sequential numbers produce objects.

decode()

Decodes a JSON string into a GSC array or object.
jsonStr = "{\"msg\": \"Hello world!\"}";
obj = unleashed\json::decode(jsonStr);
iPrintLn(obj["msg"]); // Prints: Hello world!
Parameters:
  • str - JSON string to decode
Returns: GSC array or object
The decoder supports:
  • Objects (curly braces)
  • Arrays (square brackets)
  • Strings (quoted)
  • Numbers (converted to float)
  • Booleans (true/false)
  • Nested structures

Usage Examples

Saving Player Data

savePlayerData(player) {
    data = [];
    data["name"] = player.name;
    data["guid"] = player.guid;
    data["stats"] = [];
    data["stats"]["kills"] = player.pers["kills"];
    data["stats"]["deaths"] = player.pers["deaths"];
    
    jsonStr = unleashed\json::encode(data);
    // Save jsonStr to file or database
    return jsonStr;
}

Loading Configuration

loadConfig() {
    // Read JSON string from file
    jsonStr = readConfigFile("config.json");
    
    config = unleashed\json::decode(jsonStr);
    
    level.maxScore = config["maxScore"];
    level.timeLimit = config["timeLimit"];
    level.enableKillstreaks = config["features"]["killstreaks"];
}

API Communication

sendToAPI(endpoint, data) {
    jsonPayload = unleashed\json::encode(data);
    
    response = httpPost(endpoint, jsonPayload);
    
    if (isDefined(response)) {
        result = unleashed\json::decode(response);
        return result;
    }
    return undefined;
}

Implementation Details

Escaping: The JSON module does not automatically escape special characters in strings. Be cautious when encoding strings containing quotes or backslashes.

Type Handling

GSC TypeJSON Encoding
String"string"
Number42 or 3.14
Booleantrue or false
Array (sequential)[...]
Array (associative){...}

Array vs Object Detection

The encoder determines whether to create a JSON array or object by examining the keys:
// Creates JSON array
arr[0] = "a";
arr[1] = "b";
arr[2] = "c";
// Result: ["a","b","c"]

// Creates JSON object
obj["first"] = "a";
obj["second"] = "b";
// Result: {"first":"a","second":"b"}

// Creates JSON object (non-sequential)
arr[0] = "a";
arr[5] = "b";
// Result: {"0":"a","5":"b"}

See Also

Build docs developers (and LLMs) love