Module:GetJSON: Difference between revisions

From AlternateWiki
92 bytes removed ,  1 month ago
no edit summary
No edit summary
No edit summary
Line 3:
local m = {}
m.root = mw.loadJsonData(frame.args[1])
if m.root[frame.args[2]] ~= nil then
-- Function to split the key by "."
if type(m.root[frame.args[2]]) == "table" then
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
return value
end
 
local keys = splitKey(frame.args[2])
local value = getValueByKeys(m.root, keys)
if m.root[frame.args[2]]value ~= nil then
if type(m.root[frame.args[2]]value) == "table" then
local str = ""
for k,v in pairs(m.root[frame.args[2]]value) do
str = str .. v .. ";"
end
return str
end
return m.root[frame.args[2]]value
else
if frame.args[3] ~= nil then
Line 20 ⟶ 45:
end
end
return p
return p --All modules end by returning the variable containing their functions to Wikipedia.
-- Now we can use this module by calling {{#invoke: Example | hello }},
-- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=6}}
-- Note that the first part of the invoke is the name of the Module's wikipage,
-- and the second part is the name of one of the functions attached to the
-- variable that you returned.
 
-- The "print" function is not allowed in Wikipedia. All output is accomplished
-- via strings "returned" to Wikipedia.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.