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( |
p.main = function(frame) |
||
local m = {} |
|||
m.root = mw.loadJsonData(frame.args[1]) |
|||
-- Function to split the key by "." |
-- 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 |
-- 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 |
|||
end |
|||
value = value[key] |
|||
end |
|||
⚫ | |||
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
end |
|||
⚫ | |||
else |
else |
||
if frame.args[3] ~= nil then |
|||
return frame.args[3] |
|||
else |
|||
return "" |
|||
end |
|||
end |
end |
||
end |
end |