Module:GetJSON: Difference between revisions

From AlternateWiki
Content added Content deleted
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
-- Create a Lua module named "p"
local p = {}
local p = {}

-- Define the main function of the module
p.main = function(frame)
p.main = function(frame)
-- Create a local table "m" to store data
local m = {}
local m = {}
-- Load JSON data from the first argument passed to the module
m.root = mw.loadJsonData(frame.args[1])
m.root = mw.loadJsonData(frame.args[1])
-- Function to split the key by "."
-- Function to split a key string by "."
local function splitKey(key)
local function splitKey(key)
local keys = {}
local keys = {}
-- Split the key string by "."
for subkey in string.gmatch(key, "([^%.]+)") do
for subkey in string.gmatch(key, "([^%.]+)") do
-- Insert each subkey into the keys table
table.insert(keys, subkey)
table.insert(keys, subkey)
end
end
return keys
return keys -- Return the keys table
end
end
-- Function to get the value from the nested table
-- Function to get the value from the nested table using keys
local function getValueByKeys(root, keys)
local function getValueByKeys(root, keys)
local value = root
local value = root
-- Iterate through each key
for _, key in ipairs(keys) do
for _, key in ipairs(keys) do
-- Check if the key is numeric
if tonumber(key) then
if tonumber(key) then
key = tonumber(key)
key = tonumber(key) -- Convert numeric key to number
end
end
-- Check if the value associated with the key is nil
if value[key] == nil then
if value[key] == nil then
return nil
return nil -- Return nil if value is nil
end
end
value = value[key]
value = value[key] -- Update value to the nested table
end
end


-- If the value is a table and contains json_file and json_key
-- 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
if type(value) == "table" and value.json_file and value.json_key then
-- Load JSON data from the specified file
local newRoot = mw.loadJsonData(value.json_file)
local newRoot = mw.loadJsonData(value.json_file)
-- Split the json_key string
local newKeys = splitKey(value.json_key)
local newKeys = splitKey(value.json_key)
-- Recursively call this function with new data
return getValueByKeys(newRoot, newKeys)
return getValueByKeys(newRoot, newKeys)
end
end
return value
return value -- Return the final value
end
end


-- Split the key provided in the second argument
local keys = splitKey(frame.args[2])
local keys = splitKey(frame.args[2])
-- Get the value from the nested table using the keys
local value = getValueByKeys(m.root, keys)
local value = getValueByKeys(m.root, keys)
-- If the value is not nil
if value ~= nil then
if value ~= nil then
-- If the value is a table
if type(value) == "table" then
if type(value) == "table" then
-- If the fourth parameter is "keys"
if frame.args[4] == "keys" then
if frame.args[4] == "keys" then
-- Return the keys of the table
-- Return keys of the table
local str = ""
local str = ""
for k, _ in pairs(value) do
for k, _ in pairs(value) do
Line 50: Line 68:
return frame:preprocess(str:sub(1, -2))
return frame:preprocess(str:sub(1, -2))
else
else
-- Return the values of the table
-- Return values of the table
local str = ""
local str = ""
for _, v in pairs(value) do
for _, v in pairs(value) do
Line 58: Line 76:
end
end
end
end
return frame:preprocess(value)
return frame:preprocess(value) -- Return the value
else
else
-- If the third argument is not nil, return it
if frame.args[3] ~= nil then
if frame.args[3] ~= nil then
return frame.args[3]
return frame.args[3]
else
else
return ""
return "" -- Return an empty string if no value is found
end
end
end
end
end
end

-- Return the module table "p"
return p
return p