Using a variable as method

I am working with some JSON data from a website.Problem is, it is organized data. It looks like this:

word: {“1”: data, “2”: data}
word2: {“1”: data, “2”: data}
word3: {“1”: data, “2”: data}

and so on. basically, it is a multi dimensional array. Now, to access the data, i should refer to it as (assuming JSON is the object name) JSON.word[2] and JSON.word2[1].
Here is where my problem comes from: the data is dynamic, meaning all the indexes depend on the values of the database, except the number indexes. Since the website outputting the JSON data is mine, i set it up like so: the values numbered in the root of the JSON are the indexes, then each index has 12 values associated with it. Basically, i cannot do JSON.value because i don’t always know the actual values, but i have them stored in an array (using a for cycle cyclying thru the numeric indexes of the JSON). So, is there a way to use a variable as an attribute call? If i want to access JSON.word2[1], can i somehow assign the value “word2” to a variable and go JSON.variable[1]?

Thanks in advance.

Yes, you can use a variable as a key to access values ​​inside a JSON object in Lua. In Lua, JSON objects are handled as tables, and you can access their attributes dynamically using the [ ] bracket notation. This is very useful for cases like the one you describe, where the key names are not static.

Here is a practical example:

-- Suppose you have a JSON like this:
local JSON = {
    word = { ["1"] = "data1", ["2"] = "data2" },
    word2 = { ["1"] = "data3", ["2"] = "data4" },
    word3 = { ["1"] = "data5", ["2"] = "data6" }
}

-- You can store the key name in a variable:
local key = "word2"

-- And then dynamically access the corresponding value:
local value = JSON[key]["1"] -- Esto será "data3"
print(value) -- Salida: data3

-- If the key names are dynamic, you can iterate over them using pairs:
for k, v in pairs(JSON) do
    print("Key:", k)
    for index, data in pairs(v) do
        print("Index:", index, "Data:", data)
    end
end

One thing to keep in mind in the above example is that
JSON[key]["1"]
and
JSON[key][1]
are not the same.
This is particularly important to know when dealing with JSON to Lua conversions, because the default behaviour is that if the indices in the object are a mix of numeric and non-numeric keys they will all be decoded as string keys. They will only be decoded as numeric keys if all keys are numeric.

So this
{ [1] = "data1", [2] = "data2", ["some"] = "string"}
would be decoded as if it was originally:
{ ["1"] = "data1", ["2"] = "data2", ["some"] = "string"}

But this
{ [1] = "data1", [2] = "data2", [3] = "string"}
would remain as:
{ [1] = "data1", [2] = "data2", [3] = "string"}

I always pass my decoded table through a function that checks if a key could be numeric, and reindexes it if so:

local function toNumericKeys(t)
    local t2 = {}
        for k,v in pairs(t) do
        	if tonumber(k) and type(k) ~= "number" then
        		local k_num = tonumber(k)
        		local storeV = v
        		t2[k] = nil

	        	if type(v) == "table" then
				    t2[k_num] = jsonHandler.toNumericKeys(v)
				else
        	    	t2[k_num] = storeV
        	    end
        	else
            	if type(v) == "table" then
    			    t2[k] = jsonHandler.toNumericKeys(v)
    			else
    				t2[k] = v
    			end
        	end  
        end
    return t2
end

local jsonDecoded = json.decode("myJsonString.json")
local reindexed = toNumericKeys(jsonDecoded)
1 Like