Module:FilterJsonById: Difference between revisions

From AoP Wiki
Added FilterJsonById Module
 
Testing
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local http = require('mw.http')
-- Module:JsonDataParser
local json = require('cjson')


local p = {}
local json = mw.json  -- Import the JSON decoder


function p.filterById(frame)
-- Example JSON data (replace with actual content, or load via an external method)
    -- Fetch the JSON data from the URL
local jsonData = [[
    local url = frame.args.url
  {
     local response = http.get(url)
    "Armor_Trait_1": 6,
    "Armor_DTExplode": 4,
    "Armor_CrTypeFemale": 7,
    "Tradeability": 4,
    "Armor_AC": 0,
    "Material": 1,
    "Armor_Trait_3": 8,
    "Description": "Polished metal plates, crudely forming a suit of armor.",
    "Armor_CrTypeMale": 14,
    "Deteriorable": 1,
    "Subtype": 4,
    "Armor_DRExplode": 15,
    "Flags": 134217758,
    "GroundLevel": 1,
    "Armor_DTPlasma": 5,
    "Armor_DTElectr": 0,
    "Armor_DRLaser": 60,
    "Armor_DTFire": 3,
    "PicInv": "art/armor/mtlarmor.png",
    "Armor_DREmp": 500,
    "ProtoId": 2,
    "Armor_DRNormal": 35,
    "DisableEgg": 1,
    "SoundId": 48,
     "Armor_Trait_4": 8,
    "Armor_Trait_2": 8,
    "PicMap": "art/items/ground_metalarmor.fofrm",
    "Armor_Trait_0": 3,
    "Armor_DTNormal": 13,
    "Type": 1,
    "Armor_DRPlasma": 10,
    "Armor_DRElectr": 0,
    "Armor_Trait_5": 25,
    "Cost": 0,
    "Name": "Metal Armor",
    "Armor_DTLaser": 8,
    "Armor_DRFire": 5,
    "Tier": 1,
    "Weight": 15000
  }
]]


    -- If there's a response
-- local data = json.decode(jsonData)  -- Decode the JSON content into a Lua table
    if response then
        -- Decode the JSON response into a Lua table
        local data = json.decode(response)


        -- Retrieve the ID passed to the module
-- Create a table of extracted data
        local targetId = frame.args.id
--[[ local resultTable = {
  name = data.Weight,
  age = data.Weight,
  location = data.Weight,
  occupation = data.Weight
}
]]--


        -- Table to hold the filtered results
local exampleTable = {
        local filteredData = {}
  name = "John",
  age = 25,
  location = "New York"
}


        -- Loop through the entries in the JSON
mw.log("Debugging table: " .. mw.dumpObject(exampleTable))
        for _, entry in ipairs(data) do
            -- Check if the 'id' matches the target ID
            if entry.ProtoId == targetId then
                table.insert(filteredData, entry)
            end
        end


        -- If we found matching entries, format the result
        if #filteredData > 0 then
            local result = ""
            for _, entry in ipairs(filteredData) do
                result = result .. "<div>Entry ID: " .. entry.id .. "</div>"
                result = result .. "<div>Name: " .. entry.name .. "</div>"
                -- Add other fields as needed
            end
            return result
        else
            return "No entries found with the specified ID."
        end
    else
        return "Error: Could not fetch data."
    end
end


return p
-- Return the table to be used in the wiki page
return data

Latest revision as of 09:00, 19 February 2025

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

-- Module:JsonDataParser

local json = mw.json  -- Import the JSON decoder

-- Example JSON data (replace with actual content, or load via an external method)
local jsonData = [[
  {
    "Armor_Trait_1": 6,
    "Armor_DTExplode": 4,
    "Armor_CrTypeFemale": 7,
    "Tradeability": 4,
    "Armor_AC": 0,
    "Material": 1,
    "Armor_Trait_3": 8,
    "Description": "Polished metal plates, crudely forming a suit of armor.",
    "Armor_CrTypeMale": 14,
    "Deteriorable": 1,
    "Subtype": 4,
    "Armor_DRExplode": 15,
    "Flags": 134217758,
    "GroundLevel": 1,
    "Armor_DTPlasma": 5,
    "Armor_DTElectr": 0,
    "Armor_DRLaser": 60,
    "Armor_DTFire": 3,
    "PicInv": "art/armor/mtlarmor.png",
    "Armor_DREmp": 500,
    "ProtoId": 2,
    "Armor_DRNormal": 35,
    "DisableEgg": 1,
    "SoundId": 48,
    "Armor_Trait_4": 8,
    "Armor_Trait_2": 8,
    "PicMap": "art/items/ground_metalarmor.fofrm",
    "Armor_Trait_0": 3,
    "Armor_DTNormal": 13,
    "Type": 1,
    "Armor_DRPlasma": 10,
    "Armor_DRElectr": 0,
    "Armor_Trait_5": 25,
    "Cost": 0,
    "Name": "Metal Armor",
    "Armor_DTLaser": 8,
    "Armor_DRFire": 5,
    "Tier": 1,
    "Weight": 15000
  }
]]

-- local data = json.decode(jsonData)  -- Decode the JSON content into a Lua table

-- Create a table of extracted data
--[[ local resultTable = {
  name = data.Weight,
  age = data.Weight,
  location = data.Weight,
  occupation = data.Weight
}
]]--

local exampleTable = {
  name = "John",
  age = 25,
  location = "New York"
}

mw.log("Debugging table: " .. mw.dumpObject(exampleTable))


-- Return the table to be used in the wiki page
return data