Module:Lua json: Difference between revisions

From Visual Data Wiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = require('Module:Http')
local p = {}
local json = require('Module:Json')


local function queryPages(titleParam, titleValue)
function p.getProjectData()
    local url = mw.uri.fullUrl('api.php', {action = 'query', format = 'json', list = 'allpages', apnamespace = 486})
     local jsonPage = mw.title.new("Entry:Datasets/Dataset")
    local response = p.get(url)
    if jsonPage and jsonPage.exists then
     local data = json.decode(response)
        local content = jsonPage:getContent()
 
        local jsonData = mw.text.jsonDecode(content)
    if not data or not data.query or not data.query.allpages then
        if jsonData and jsonData.schemas and jsonData.schemas.Dataset then
        return {}
            return jsonData.schemas.Dataset.date
    end
        else
 
             return "Error: Invalid JSON structure"
    local pages = {}
    for _, page in ipairs(data.query.allpages) do
        table.insert(pages, page.title)
    end
 
    return pages
end
 
function p.getDataByTitle(titleParam, titleValue, targetParam)
    local pages = queryPages(titleParam, titleValue)
    local results = {}
 
    for _, pageTitle in ipairs(pages) do
        local jsonPage = mw.title.new(pageTitle)
        if jsonPage and jsonPage.exists and jsonPage.contentModel == 'json' then
            local content = jsonPage:getContent()
            local jsonData = json.decode(content)
            if jsonData and jsonData[titleParam] == titleValue then
                table.insert(results, jsonData[targetParam])
             end
         end
         end
    else
        return "Error: JSON page does not exist"
     end
     end
    return results
end
end


return p
return p

Latest revision as of 10:33, 2 May 2024

Documentation for this module may be created at Module:Lua json/doc

local p = {}

function p.getProjectData()
    local jsonPage = mw.title.new("Entry:Datasets/Dataset")
    if jsonPage and jsonPage.exists then
        local content = jsonPage:getContent()
        local jsonData = mw.text.jsonDecode(content)
        if jsonData and jsonData.schemas and jsonData.schemas.Dataset then
            return jsonData.schemas.Dataset.date
        else
            return "Error: Invalid JSON structure"
        end
    else
        return "Error: JSON page does not exist"
    end
end

return p