Skip to content

pyio.parse_json

Daniel Flassig edited this page May 12, 2025 · 10 revisions

Parses a json file to a basic Lua data structure.

pyio.parse_json(file) 
Parameter Type Description
file file handle The file that should be read

Return value

json-Type Lua equivalent
json object {...} Lua table with strings as keys (see example)
json array [...] Lua table with integer keys starting at 1(!)
json string "..." Lua string
json number Lua number
json true, false, null Lua true, false, nil

Example:

For example, a json file with the following content

{ 
  "Type" : "TEST", 
  "My Numbers" : [1, 2, 3.5] 
}

would result in the following lua tables

{ 
  Type = "Test", 
  ["My Numbers"] = {1, 2, 3.5} 
}

Remarks:

Interestingly, json can be mapped quite easily to the data types available in Lua. Please note, however, that the json array indices t = ["a", "b"] would be 0-based when accessed in javascript (i.e. t[0] = "a"; t[1] = "b";). Whereas the resulting table in Lua t_lua = {"a", "b"} has 1-based indices (i.e. t_lua[1] = "a"; t_lua[2] = "b";).

See also:

pyio, pyio.parse_xml

Clone this wiki locally