toJSON | Multi Theft Auto: Wiki Skip to content

toJSON

Client-side
Server-side
Shared

This page is incomplete! Help wanted!

Please finish this page using the corresponding Old Wiki article.
Go to Contribution guidelines for more information.


This function converts a single value (preferably a Lua table) into a JSON encoded string. You can use this to store the data and then load it again using fromJSON.

Caution

When using toJSON for submitting data using fetchRemote for example, make sure to use string.sub(data, 2, -2) to remove the brackets as many APIs will not understand the request

Important

Due to technical limitations (partly of json-c) the stringified keys will be truncated to the first 255 characters

Syntax

toJSON ( )

Code Examples

server

This example shows how you can encode an array. The string json should equal"[ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]" after executed.

local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } )