Module:GetJSON: Difference between revisions
From AlternateWiki
Content deleted Content added
No edit summary |
No edit summary Tag: Manual revert |
||
(17 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
-- Create a Lua module named "p" |
|||
local p = {}; |
|||
local p = {} |
|||
p.main = function( frame ) |
|||
local m = {} |
|||
-- Define the main function of the module |
|||
m.root = mw.loadJsonData(frame.args[1]) |
|||
p.main = function(frame) |
|||
-- Create a local table "m" to store data |
|||
local m = {} |
|||
-- Load JSON data from the first argument passed to the module |
|||
m.root = mw.loadJsonData(frame.args[1]) |
|||
-- Function to split |
-- Function to split a key string by "." |
||
local function splitKey(key) |
|||
local keys = {} |
|||
-- Split the key string by "." |
|||
for subkey in string.gmatch(key, "([^%.]+)") do |
|||
for subkey in string.gmatch(key, "([^%.]+)") do |
|||
table.insert(keys, subkey) |
|||
-- Insert each subkey into the keys table |
|||
end |
|||
table.insert(keys, subkey) |
|||
return keys |
|||
end |
|||
return keys -- Return the keys table |
|||
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 value = root |
|||
for _, key in ipairs(keys) do |
|||
function pairsByKeys (t, f) |
|||
if value[key] == nil then |
|||
local a = {} |
|||
return nil |
|||
for n in pairs(t) do table.insert(a, n) end |
|||
end |
|||
table.sort(a, f) |
|||
value = value[key] |
|||
local i = 0 -- iterator variable |
|||
local iter = function () -- iterator function |
|||
i = i + 1 |
|||
if a[i] == nil then return nil |
|||
else return a[i], t[a[i]] |
|||
end |
|||
end |
|||
return iter |
|||
end |
end |
||
return value |
|||
-- Iterate through each key |
|||
end |
|||
for _, key in ipairs(keys) do |
|||
if key[0] == "#" then |
|||
keyval = tonumber(string.sub(key, 2)) |
|||
i = 1 |
|||
for name, line in pairsByKeys(value) do |
|||
if i == keyval then |
|||
return value[name] |
|||
end |
|||
i = i + 1 |
|||
end |
|||
end |
|||
-- Check if the key is numeric |
|||
if tonumber(key) then |
|||
key = tonumber(key) -- Convert numeric key to number |
|||
end |
|||
-- Check if the value associated with the key is nil |
|||
if value[key] == nil then |
|||
return nil -- Return nil if value is nil |
|||
end |
|||
value = value[key] -- Update value to the nested table |
|||
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) |
|||
-- Load JSON data from the specified file |
|||
local newRoot = mw.loadJsonData(value.json_file) |
|||
if value ~= nil then |
|||
-- Split the json_key string |
|||
if type(value) == "table" then |
|||
local newKeys = splitKey(value.json_key) |
|||
local str = "" |
|||
-- Recursively call this function with new data |
|||
for k,v in pairs(value) do |
|||
return getValueByKeys(newRoot, newKeys) |
|||
str = str .. v .. ";" |
|||
end |
|||
return str:sub(1, -2) |
|||
return value -- Return the final value |
|||
end |
|||
end |
|||
-- Split the key provided in the second argument |
|||
local keys = splitKey(frame.args[2]) |
|||
-- Get the value from the nested table using the keys |
|||
local value = getValueByKeys(m.root, keys) |
|||
-- If the value is not nil |
|||
if value ~= nil then |
|||
-- If the value is a table |
|||
if type(value) == "table" then |
|||
-- If the fourth parameter is "keys" |
|||
if frame.args[4] == "keys" then |
|||
-- Return 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 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) -- 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] |
|||
else |
|||
else |
|||
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 |
Latest revision as of 18:43, 21 June 2024
Documentation for this module may be created at Module:GetJSON/doc
-- Create a Lua module named "p"
local p = {}
-- Define the main function of the module
p.main = function(frame)
-- Create a local table "m" to store data
local m = {}
-- Load JSON data from the first argument passed to the module
m.root = mw.loadJsonData(frame.args[1])
-- Function to split a key string by "."
local function splitKey(key)
local keys = {}
-- Split the key string by "."
for subkey in string.gmatch(key, "([^%.]+)") do
-- Insert each subkey into the keys table
table.insert(keys, subkey)
end
return keys -- Return the keys table
end
-- Function to get the value from the nested table using keys
local function getValueByKeys(root, keys)
local value = root
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
-- Iterate through each key
for _, key in ipairs(keys) do
if key[0] == "#" then
keyval = tonumber(string.sub(key, 2))
i = 1
for name, line in pairsByKeys(value) do
if i == keyval then
return value[name]
end
i = i + 1
end
end
-- Check if the key is numeric
if tonumber(key) then
key = tonumber(key) -- Convert numeric key to number
end
-- Check if the value associated with the key is nil
if value[key] == nil then
return nil -- Return nil if value is nil
end
value = value[key] -- Update value to the nested table
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
-- Load JSON data from the specified file
local newRoot = mw.loadJsonData(value.json_file)
-- Split the json_key string
local newKeys = splitKey(value.json_key)
-- Recursively call this function with new data
return getValueByKeys(newRoot, newKeys)
end
return value -- Return the final value
end
-- Split the key provided in the second argument
local keys = splitKey(frame.args[2])
-- Get the value from the nested table using the keys
local value = getValueByKeys(m.root, keys)
-- If the value is not nil
if value ~= nil then
-- If the value is a table
if type(value) == "table" then
-- If the fourth parameter is "keys"
if frame.args[4] == "keys" then
-- Return 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 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) -- Return the value
else
-- If the third argument is not nil, return it
if frame.args[3] ~= nil then
return frame.args[3]
else
return "" -- Return an empty string if no value is found
end
end
end
-- Return the module table "p"
return p