toJSON

From Bohemia Interactive Community
Revision as of 17:34, 4 November 2024 by Ansin11 (talk | contribs) (Small page overhaul)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Hover & click on the images for description

Description

Description:
Converts data to JSON.
Groups:
Strings

Syntax

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
    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.
Return Value:
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 4:
private _jsonPlayer = toJSON player; // empty string - Object is not supported

Additional Information

See also:
fromJSON toString str

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note