Description
Converts data to JSON.
Syntax
toJSON data
Parameters:
-
data- Only certain Data Types are supported:- Boolean - serialized as JSON boolean
- Number - serialized as JSON number (floating-point)
- String - serialized as JSON string
- Array - Serialized as JSON array. Unsupported elements (e.g. Objects) are converted to JSON null.
- HashMap - Serialized as JSON object. Only String keys are supported, other keys are ignored. Unsupported values (e.g. Objects) are converted to JSON null.
- nil - serialized as JSON null
Important
The nil value can only be passed as a member of an Array or HashMap. It cannot be passed directly as the command would simply not execute.
Returns: String - JSON string
Examples
Example 1
private _result = toJSON ["this", "is", "an", "array"];
Result:
["this","is","an","array"]
Example 2
private _hashMap = createHashMap;
_hashMap set ["bKey", true];
_hashMap set ["sKey", "Hello there"];
_hashMap set ["oKey", player];
_hashMap set ["nKey", 42];
_hashMap set ["hKey", ["key1", "key2", "yolo"] createHashMapFromArray ["General Kenobi", true, "Hello it's me"]];
toJSON _hashMap;
Result (formatted for readability):
{
"bKey": true,
"sKey": "Hello there",
"oKey": null,
"nkey": 42,
"hKey": {
"key1": "General Kenobi",
"key2": true,
"yolo": "Hello it's me"
}
}
Example 3
private _jsonPlayer = toJSON player; // empty string - [[Object]] is not supported