-
Notifications
You must be signed in to change notification settings - Fork 18
pyio.parse_json
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 |
| json-Type | Lua equivalent |
|---|---|
json object {...}
|
Lua table with string-keys (see example) |
json array [...]
|
Lua table with integer indices starting at 1(!) |
json string "..."
|
Lua string |
| json number | Lua number |
json true, false, null
|
Lua true, false, nil
|
For example, a text 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}
}The encoding of the text-file is automatically detected. UTF8, UTF8 with BOM, UTF16LE with BOM and ASCII are supported. If the encoding can be chosen freely, the either UTF8 with BOM or UTF16 with BOM are recommended. For UTF8 without BOM / ASCII, there's a small risk that the encoding might not be detected correctly.
For line endings, Windows and Unix line endings (CR+LF and LF) are equally supported.
The default field separator is the comma ,. Quoted fields are supported. Separators (commas) and newlines are allowed in quoted fields. To include a literal quotation mark " in a quoted field, use two "". For details, see RFC4180.
Note that is some locales (e.g. Germany) some popular programs (e.g. Excel) will use semicolons instead of commas in the csv. It also uses a comma as a decimal separator in numbers, there (3,14). The function parse_csv() does not normalize these numbers automatically. Use string.gsub(field, ",", ".") to normalize these fields.