This resource makes it possible to manage JSON files through simple exports.
Github:
Usage
This is the same ReadMe.md that is on the github page.
Json handler for FiveM
Json handler is a resource which is capable of writing and getting data from json files.
Usage
There are 2 methods for calling the functions in this resource from anywhere.
-
Using events. There are 3 server events declared in the resourece which are:
-
json:getFile Expects 1 variable. The
filename(without extension) and it returns acbvalue.Examples:
TriggerEvent("json:getFile", "users", function(cb) print(cb[steamid].name) end)This example returns the name according to the steamid from the users table.
steamidis a variable. -
json:getItem Expects 2 variables. The
filename(without extension), thename of the itemyou want and it returns acbvalue.Examples:
TriggerEvent("json:getItem", "users", steamid, function(cb) print(cb.name) end)This example print out the same as the last one, but it only returns the item that is called
steamid.steamidis a variable -
json:addItem Expects 3 variables. The
filename(without extension), thename of the itemyou want to create and alua tablewith the data of your item, and their value.Examples:
TriggerEvent("json:addItem", "users", steamid, { name = "example", example = 5 })This example creates an item in
users.jsoncalled steamid with the givem parameters.steamidis a variable -
json:deleteItem Expects 2 variables. The
filename(without extension) and thename of the itemyou want to delete.Examples:
TriggerEvent("json:deleteItem", "users", steamid)This example deletes the
steamiditem fromusers.json. -
json:replaceItem Expects 3 variables. The
filename(without extension), thename of the itemyou want to replace and alua tablewith the data of your item, and their value.Examples:
TriggerEvent("json:replaceItem", "users", steamid, { name = "example", example = 6 })This example replaces an item in
users.jsoncalledsteamidwith the givem parameters.steamidis a variable -
json:replaceData Expects 3 variables. The
filename(without extension), thename of the itemyou want to replace in and alua tablewith another lua table inside it. The second table has 2 variables. name and content. You will understand it form the example.Examples:
TriggerEvent("json:replaceData", "users", steamid, { { name = "name", content = "bence" }, { name = "money", content= 10 } })This example replaces the
nameand themoneyinusers.json
steamidis a variable-
json:deleteData Expects 3 variables. The
filename(without extension), thename of the itemyou want to do the delete in and the name of the data you want to delete.Examples:
TriggerEvent("json:deleteData", "users", steamid, "money")This example deletes the data named
moneyfrom thesteamiditem inusers.jsonin the
steamiditem with the corresponding content.
-
-
Using exports. There is an option to import a class using exports.
The class work like this:
local file = exports.json_handler:InitiateFile(filename)Then if you copied in this line, you can access 7 functions. These are:
-
file.GetFile()
-
file.GetItem(itemname)
-
file.AddItem(itemname, content)
-
file.DeleteItem(itemname)
-
file.ReplaceItem(itemname, itemcontent)
-
file.ReplaceData(itemname, table)
-
file.DeleteData(itemname, dataname)
Note that you can use whatever variable name you want
Note that these functions do the same, as the events mentioned above. If you do not understand what they do, have a look at the corresponding event.
I suggest using exports if you executing the request on the server side, becouse they are more resource friendly.
-
Note that files need to be created by the developer, who plans to use this, and it can contain anything that json can. I used users in the examples, but you might not have that file.
copyright © 0324bence