# json.encode (Shared)

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

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

## Syntax

```lua
string json.encode(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 = json.encode({ x = 1.5, tags = { "a", "b" } }, true)
print(text)
```

### Note

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

## See also

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