Module:GetJSON: Difference between revisions

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


-- If the value is a table and contains json_file and json_key
local keys = splitKey(frame.args[2])
if type(value) == "table" and value.json_file and value.json_key then
local value = getValueByKeys(m.root, keys)
local newRoot = mw.loadJsonData(value.json_file)
local newKeys = splitKey(value.json_key)
if value ~= nil then
return getValueByKeys(newRoot, newKeys)
if type(value) == "table" then
end
local str = ""
for k,v in pairs(value) do
return value
str = str .. v .. ";"
end
end

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

Revision as of 18:14, 20 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
            local str = ""
            for k, v in pairs(value) do
                str = str .. v .. ";"
            end
            return frame:preprocess(str:sub(1, -2))
        end
        return frame:preprocess(value)
    else
        if frame.args[3] ~= nil then
            return frame.args[3]
        else
            return ""
        end
    end
end
return p