---
name: "toJSON"
type: command
status: ok
groups: ["Strings"]
arma3_since: "2.18"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/toJSON
revid: 370177
updated: 2024-11-04T16:34:52Z
---

# toJSON

*Command · Strings · Arma 3 v2.18*

## Description

Converts data to [JSON](https://en.wikipedia.org/wiki/JSON).

## Syntax

```sqf
toJSON data
```

**Parameters:**

- `data` - Only certain Data Types are supported:
  - [Boolean](../topics/Boolean.md) - serialized as JSON boolean
  - [Number](../topics/Number.md) - serialized as JSON number (floating-point)
  - [String](../topics/String.md) - serialized as JSON string
  - [Array](../topics/Array.md) - Serialized as JSON array. Unsupported elements (e.g. [Object](../topics/Object.md)s) are converted to JSON null.
  - [HashMap](../topics/HashMap.md) - Serialized as JSON object. Only [String](../topics/String.md) keys are supported, other keys are ignored. Unsupported values (e.g. [Object](../topics/Object.md)s) are converted to JSON null.
  - [nil](../commands/nil.md) - serialized as JSON null 

  > [!IMPORTANT]
  > The [nil](../commands/nil.md) value can only be passed as a member of an [Array](../topics/Array.md) or [HashMap](../topics/HashMap.md). It cannot be passed directly as the command would simply not execute.

**Returns:** [String](../topics/String.md) - JSON string

## Examples

### Example 1

```sqf
private _result = toJSON ["this", "is", "an", "array"];
```

Result:

```json
["this","is","an","array"]
```

### Example 2

```sqf
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):

```json
{
    "bKey": true,
    "sKey": "Hello there",
    "oKey": null,
    "nkey": 42,
    "hKey": {
        "key1": "General Kenobi",
        "key2": true,
        "yolo": "Hello it's me"
    }
}
```

### Example 3

```sqf
private _jsonPlayer = toJSON player; // empty string - [[Object]] is not supported
```

## See also

[fromJSON](../commands/fromJSON.md) · [toString](../commands/toString.md) · [str](../commands/str.md)

<sub>Source: [BI Community Wiki — toJSON](https://community.bistudio.com/wiki/toJSON) (revision 370177).</sub>
