toJSON | Multi Theft Auto: Wiki Skip to content

toJSON

Client-side
Server-side
Shared

Manual Review Required

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

string toJSON ( var value, [ bool compact = false, string prettyType = "none" ] )
Required Arguments
  • value: MISSING_PARAM_DESC
Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use.

  • compact (default: false): a boolean representing whether the string will contain whitespaces. To remove whitespaces from JSON string, use true . String will contain whitespaces per default.
  • prettyType (default: "none"): MISSING_PARAM_DESC

Returns

  • string: value

Returns a JSON formatted string.

Code Examples

shared

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 } )