Module:GetJSON: Difference between revisions

From AlternateWiki
Content added Content deleted
No edit summary
No edit summary
Tag: Reverted
Line 20: Line 20:
return nil
return nil
end
end
value = value[key]
value = value[key]:gsub("#", "{{Hash-tag}}")
end
end
return value
return value
Line 34: Line 34:
str = str .. v .. ";"
str = str .. v .. ";"
end
end
return str:sub(1, -2)
return str:sub(1, -2):gsub("#", "{{Hash-tag}}")
end
end
return value
return value

Revision as of 00:51, 16 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]:gsub("#", "{{Hash-tag}}")
		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 str:sub(1, -2):gsub("#", "{{Hash-tag}}")
		end
    	return value
    else
    	if frame.args[3] ~= nil then
    		return frame.args[3]
    	else
    		return ""
    	end
    end
end
return p