Module:GetJSON: Difference between revisions

From AlternateWiki
Content added Content deleted
No edit summary
No edit summary
Line 31: Line 31:
return value
return value
end
-- Function to check if a table is an array
local function isArray(table)
local count = 0
for k, _ in pairs(table) do
if type(k) ~= "number" then
return false
end
count = count + 1
end
return #table == count
end
end


Line 50: Line 38:
if value ~= nil then
if value ~= nil then
if type(value) == "table" then
if type(value) == "table" then
if isArray(value) then
if frame.args[4] == "keys" then
-- Return the keys of the table
local str = ""
local str = ""
for _, v in ipairs(value) do
for k, _ in pairs(value) do
str = str .. v .. ";"
str = str .. k .. ";"
end
end
return frame:preprocess(str:sub(1, -2))
return frame:preprocess(str:sub(1, -2))
else
else
-- Return the values of the table
local str = ""
local str = ""
for k, _ in pairs(value) do
for _, v in pairs(value) do
str = str .. k .. ";"
str = str .. v .. ";"
end
end
return frame:preprocess(str:sub(1, -2))
return frame:preprocess(str:sub(1, -2))

Revision as of 23:11, 24 May 2024

Documentation for this module may be created at Module:GetJSON/doc

local p = {}
p.main = function(frame)
    local m = {}
    m.root = mw.loadJsonData(frame.args[1])
    
    -- Function to split the key by "."
    local function splitKey(key)
        local keys = {}
        for subkey in string.gmatch(key, "([^%.]+)") do
            table.insert(keys, subkey)
        end
        return keys
    end
    
    -- Function to get the value from the nested table
    local function getValueByKeys(root, keys)
        local value = root
        for _, key in ipairs(keys) do
            if value[key] == nil then
                return nil
            end
            value = value[key]
        end

        -- If the value is a table and contains json_file and json_key
        if type(value) == "table" and value.json_file and value.json_key then
            local newRoot = mw.loadJsonData(value.json_file)
            local newKeys = splitKey(value.json_key)
            return getValueByKeys(newRoot, newKeys)
        end
        
        return value
    end

    local keys = splitKey(frame.args[2])
    local value = getValueByKeys(m.root, keys)
    
    if value ~= nil then
        if type(value) == "table" then
            if frame.args[4] == "keys" then
                -- Return the keys of the table
                local str = ""
                for k, _ in pairs(value) do
                    str = str .. k .. ";"
                end
                return frame:preprocess(str:sub(1, -2))
            else
                -- Return the values of the table
                local str = ""
                for _, v in pairs(value) do
                    str = str .. v .. ";"
                end
                return frame:preprocess(str:sub(1, -2))
            end
        end
        return frame:preprocess(value)
    else
        if frame.args[3] ~= nil then
            return frame.args[3]
        else
            return ""
        end
    end
end
return p