Module:Test
Documentation for this module may be created at Module:Test/doc
local json = require("json")
-- Function to read data from JSON file
local function readJSON(filename)
local file = io.open(filename, "r") -- Open file in read mode
if not file then return nil end -- If file doesn't exist, return nil
local content = file:read("*a") -- Read the entire content
file:close() -- Close the file
return json.decode(content) -- Decode JSON content and return
end
-- Main function
local function main()
local filename = "data.json" -- JSON file name
local data = readJSON(filename) -- Read data from JSON file
if not data then
print("Error: Could not read JSON file")
return
end
-- Take input from user
io.write("Enter the name of the item: ")
local itemName = io.read()
-- Search for the item in the data
local price = data[itemName]
if price then
print("Price of", itemName, ":", price)
else
print("Item not found")
end
end
main() -- Run the main function