FromJSON: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(13 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
{{New feature/item|3.0120|1.2||
This function parses a [[JSON]] formatted string and unpacks the values into Lua variables. You can use [[toJSON]] to encode variables into a JSON string that can be read by this function.
Available client side in 1.2 and onwards.
 
{{Important Note|1=When parsing a JSON array, [[fromJSON]] unpacks the elements as multiple separate values. To mimic standard JSON parsing behavior and store these elements together in a single Lua table, you must wrap the function call in a table constructor {}.
If your JSON string was generated directly by [[toJSON]], you do not need to wrap it.
<syntaxhighlight lang="lua">
local jsonString = '["Apple", "Orange"]'
local fruitsTable = { fromJSON(jsonString) }
</syntaxhighlight>
 
}}
}}
{{MessageBox|
  bordercolorhex = FFADAD |
  bordertype = |
  bgcolorhex = F9F9F9 |
  image = File:Dialog-warning.png |
  title = Warning: |
  message = There is a known bug with this described in the mantis issue 6613 |
  subtext = http://bugs.mtasa.com/view.php?id=6613 |
}}
This function parses a [[JSON]] formatted string into variables. You can use [[toJSON]] to encode variables into a JSON string that can be read by this function.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
var fromJSON ( string json )
var... fromJSON ( string json )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''json:''' A JSON formatted string
*'''json:''' A JSON formatted string.


===Returns===
===Returns===
Returns variables read from the JSON string.
Returns the parse JSON data as multiple values or a single table depending on the input JSON structure:
* Returns '''multiple separate values''' (unpacked) if the root element of the JSON is an array
* Returns a single table if the root element of the JSON is an object
* Returns [[nil]] if the JSON string is invalid or malformed.
 
==Examples==
 
===Example 1: Assigning to multiple variables===
This example shows how fromJSON extracts values from a JSON array directly into separate Lua variables.
<syntaxhighlight lang="lua">
local jsonArray = '["Desert Eagle", 24]'
 
local name, ammo = fromJSON(jsonArray)


'''Note:''' Indices of a JSON object such as "1": "cat" are being returned as [[string]], not as [[int]]eger.
print(name, ammo) --> Output: Desert Eagle  24
</syntaxhighlight>


==Example==  
===Example 2: Wrapping the call inside a table===
This makes data equal: ''{ ["1"] = "cat", ["2"] = "mouse", ["3"] = 5, ["4"] = null, ["cat"] = 5, ["mouse"] =1 }''
This example shows how to capture all values of a JSON array in a single Lua table.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local data = fromJSON ( '[ { "1": "cat", "2": "mouse", "3": 5, "4": null, "cat":5, "mouse":1 } ]' )
local jsonArray = '["Apple", "Orange"]'
 
-- Put the fromJSON call inside of a table directly to capture all the values of the JSON array
local fruits = {fromJSON(jsonArray)}
 
print(fruits[1], fruits[2]) --> Output: Apple  Orange
</syntaxhighlight>
</syntaxhighlight>


==Example 2==
===Example 3: Parsing a JSON object===
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local name, weapon, ammo = fromJSON("[\"Desert Eagle\", 24, 147]")
local jsonObject = '{"playerName": "Alex", "score": 2500}'
 
local playerStats = fromJSON(jsonObject)
 
if playerStats then
    print(playerStats.playerName) --> Output: Alex
    print(playerStats.score)     --> Output: 2500
end
</syntaxhighlight>
</syntaxhighlight>


==Requirements==
===Example 4: Parsing a nested JSON object===
{{Requirements|1.0|1.1.1-9.03316|}}
<syntaxhighlight lang="lua">
local jsonString = '{"vehicle": "Infernus", "upgrades": {"nos": true, "paintjob": 2}}'
 
local vehicleInfo = fromJSON(jsonString)
 
if vehicleInfo then
    print(vehicleInfo.vehicle)          --> Output: Infernus
    print(vehicleInfo.upgrades.nos)      --> Output: true
    print(vehicleInfo.upgrades.paintjob) --> Output: 2
end
</syntaxhighlight>


==See Also==
==See Also==
{{Server_functions}}
{{Server_functions}}

Latest revision as of 02:12, 9 June 2026

This function parses a JSON formatted string and unpacks the values into Lua variables. You can use toJSON to encode variables into a JSON string that can be read by this function.


[[{{{image}}}|link=|]] Important Note: When parsing a JSON array, fromJSON unpacks the elements as multiple separate values. To mimic standard JSON parsing behavior and store these elements together in a single Lua table, you must wrap the function call in a table constructor {}.

If your JSON string was generated directly by toJSON, you do not need to wrap it.

local jsonString = '["Apple", "Orange"]'
local fruitsTable = { fromJSON(jsonString) }

Syntax

var... fromJSON ( string json )

Required Arguments

  • json: A JSON formatted string.

Returns

Returns the parse JSON data as multiple values or a single table depending on the input JSON structure:

  • Returns multiple separate values (unpacked) if the root element of the JSON is an array
  • Returns a single table if the root element of the JSON is an object
  • Returns nil if the JSON string is invalid or malformed.

Examples

Example 1: Assigning to multiple variables

This example shows how fromJSON extracts values from a JSON array directly into separate Lua variables.

local jsonArray = '["Desert Eagle", 24]'

local name, ammo = fromJSON(jsonArray)

print(name, ammo) --> Output: Desert Eagle  24

Example 2: Wrapping the call inside a table

This example shows how to capture all values of a JSON array in a single Lua table.

local jsonArray = '["Apple", "Orange"]'

-- Put the fromJSON call inside of a table directly to capture all the values of the JSON array
local fruits = {fromJSON(jsonArray)}

print(fruits[1], fruits[2]) --> Output: Apple  Orange

Example 3: Parsing a JSON object

local jsonObject = '{"playerName": "Alex", "score": 2500}'

local playerStats = fromJSON(jsonObject)

if playerStats then
    print(playerStats.playerName) --> Output: Alex
    print(playerStats.score)      --> Output: 2500
end

Example 4: Parsing a nested JSON object

local jsonString = '{"vehicle": "Infernus", "upgrades": {"nos": true, "paintjob": 2}}'

local vehicleInfo = fromJSON(jsonString)

if vehicleInfo then
    print(vehicleInfo.vehicle)           --> Output: Infernus
    print(vehicleInfo.upgrades.nos)      --> Output: true
    print(vehicleInfo.upgrades.paintjob) --> Output: 2
end

See Also