# toJSON (Shared)

> Turns a Lua value into JSON text. It is the same function as `json.encode`.

- **Platform**: MY Multiplayer (MYMP)
- **Kind**: Lua Function
- **Side**: Shared
- **Category**: Data and utilities
- **URL**: https://wiki.mymp.gg/en/toJSON

## Syntax

```lua
string toJSON(any value [, bool pretty = false])
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `value` | `any` | The value: `nil`, a boolean, a number, a string, a vector or a table. |
| `pretty` | `bool` | If `true`, the text is indented over several lines. *(default: `false`)* |

## Returns

The JSON text.

A table with keys 1 to n and no holes (or an empty one) becomes an array; any other becomes an object, its number keys written as strings. A vector becomes an array of numbers.

## Example

```lua
local text = toJSON({ name = "Carl", money = 350, weapons = { 22, 31 } })
-- {"name":"Carl","money":350,"weapons":[22,31]}
```

### Note

Elements, functions, NaN, infinity, keys that are neither strings nor numbers and tables nested over 32 levels raise a Lua error.

## See also

- [fromJSON (Shared)](https://wiki.mymp.gg/en/fromJSON.md): Turns JSON text into a Lua value. It is the same function as json.decode.
- [json.encode (Shared)](https://wiki.mymp.gg/en/json.encode.md): Turns a Lua value into JSON text. It is the same function as toJSON.
