Reputation: 101
I am decently new to Lua, an I have been trying to work on API calling JSON tables. However, one particular JSON Table I am trying to process has identifiers that start with numbers. For example, one such table looks like this:
"data": {
"1001": {
"plaintext": "item title",
"description": "item description",
"id": 1001
}
}
When I try to decode the json file and print "data.1001.id" to the console, for example, I keep getting an error "malformed number near '.1001.id'".
I've looked at other similar questions on this site that say to put it in square brackets, such as "data.[1001].id" or "data.[[1001]].id", but when I do that I get the error " 'name' expected near '[[1001]]'".
Any help would be appreciated
Upvotes: 1
Views: 365
Reputation: 2205
You can use normal access as an array element []:
local json = require("json")
local j=[[
{
"data": {
"1001": {
"plaintext": "item title",
"description": "item description",
"id": 1001
}
}
}
]]
local d = json.decode(j)
print(d.data["1001"].description)
Upvotes: 2