{"name":"toJSON","operator":null,"title":"toJSON","type":"command","url":"https://community.bistudio.com/wiki/toJSON","revid":370177,"updated":"2024-11-04T16:34:52Z","groups":["Strings"],"status":"ok","replaced_by":[],"games":[{"game":"arma3","name":"Arma 3","version":"2.18"}],"since":{"game":"arma3","name":"Arma 3","version":"2.18"},"arma3_since":"2.18","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Converts data to [JSON](https://en.wikipedia.org/wiki/JSON).","syntaxes":[{"syntax":"toJSON data","since":null,"effect":null,"exec":null,"params":[{"name":"data","type":null,"optional":false,"default":null,"description":"Only certain Data Types are supported:\n- Boolean - serialized as JSON boolean\n- Number - serialized as JSON number (floating-point)\n- String - serialized as JSON string\n- Array - Serialized as JSON array. Unsupported elements (e.g. Objects) are converted to JSON null.\n- HashMap - Serialized as JSON object. Only String keys are supported, other keys are ignored. Unsupported values (e.g. Objects) are converted to JSON null.\n- nil - serialized as JSON null \n\n> [!IMPORTANT]\n> 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.","types":[],"since":null,"text":"data - Only certain Data Types are supported:\n- Boolean - serialized as JSON boolean\n- Number - serialized as JSON number (floating-point)\n- String - serialized as JSON string\n- Array - Serialized as JSON array. Unsupported elements (e.g. Objects) are converted to JSON null.\n- HashMap - Serialized as JSON object. Only String keys are supported, other keys are ignored. Unsupported values (e.g. Objects) are converted to JSON null.\n- nil - serialized as JSON null \n\n> [!IMPORTANT]\n> 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":{"type":"String","types":["String"],"description":"JSON string","text":"String - JSON string"},"form":"unary","operator":null,"left":null,"right":{"text":"data","types":[]}}],"examples":[{"text":"```sqf\nprivate _result = toJSON [\"this\", \"is\", \"an\", \"array\"];\n```\n\nResult:\n\n```json\n[\"this\",\"is\",\"an\",\"array\"]\n```","code":["private _result = toJSON [\"this\", \"is\", \"an\", \"array\"];"]},{"text":"```sqf\nprivate _hashMap = createHashMap;\n_hashMap set [\"bKey\", true];\n_hashMap set [\"sKey\", \"Hello there\"];\n_hashMap set [\"oKey\", player];\n_hashMap set [\"nKey\", 42];\n_hashMap set [\"hKey\", [\"key1\", \"key2\", \"yolo\"] createHashMapFromArray [\"General Kenobi\", true, \"Hello it's me\"]];\ntoJSON _hashMap;\n```\n\nResult (formatted for readability):\n\n```json\n{\n    \"bKey\": true,\n    \"sKey\": \"Hello there\",\n    \"oKey\": null,\n    \"nkey\": 42,\n    \"hKey\": {\n        \"key1\": \"General Kenobi\",\n        \"key2\": true,\n        \"yolo\": \"Hello it's me\"\n    }\n}\n```","code":["private _hashMap = createHashMap;\n_hashMap set [\"bKey\", true];\n_hashMap set [\"sKey\", \"Hello there\"];\n_hashMap set [\"oKey\", player];\n_hashMap set [\"nKey\", 42];\n_hashMap set [\"hKey\", [\"key1\", \"key2\", \"yolo\"] createHashMapFromArray [\"General Kenobi\", true, \"Hello it's me\"]];\ntoJSON _hashMap;"]},{"text":"```sqf\nprivate _jsonPlayer = toJSON player; // empty string - [[Object]] is not supported\n```","code":["private _jsonPlayer = toJSON player; // empty string - [[Object]] is not supported"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["fromJSON","toString","str"],"notes":[],"categories":["Arma 3: New Scripting Commands","Arma 3: Scripting Commands","Command Group: Strings","Introduced with Arma 3 version 2.18","Scripting Commands"]}