[How to] JSON with FiveM

So, when I was learning JSON with FiveM I couldn’t find any tutorials on how to use it, so here we go.


Inserting an array into a JSON file

local loadFile= LoadResourceFile(GetCurrentResourceName(), "./fileNameHere.json")
local information = {name = "w00pi the legend", age = 17, coolness = 1000}

SaveResourceFile(GetCurrentResourceName(), "fileNameHere.json", json.encode(information), -1)

^^
That will insert the array into the json file leaving the information array in the JSON file looking like this.

{"name" : "w00pi the legend", "age" : 17, "coolness" : 1000}

How to extract JSON information

local loadFile= LoadResourceFile(GetCurrentResourceName(), "./fileNameHere.json") -- you only have to do this once in your code, i just put it in since it wont get confusing.
local extract = {}
extract = json.decode(loadFile)
SaveResourceFile(GetCurrentResourceName(), "fileNameHere.json", json.encode(extract), -1)

If you have any tips, or see a mistake please comment bellow and ill sort it out.

Thanks!

29 Likes

Perfect!

Nice Tutorial! Does it work with ESX?

yeah, works with coding in general

Thank you!

thanks

I just wanted to be the ESX joke guy sorry w00ps

5 Likes

lol, yeah.

i was like wut?

1 Like

Sorry to bump an old thread but I had a question, if I wanted to make a JSON file like:

{"playerIdentifierHere": {
    phoneNumber: "playersPhoneNumberHere"
}}

How could I do this? The example only shows how to make something with one array

You just create a Lua table and set the key to the identifier string and set it to equal an array. Then when you json encode it will look like that.

4 Likes

Nice tutorial.

e[91mSCRIPT ERROR: citizen:/scripting/lua/json.lua:397: bad argument #1 to ‘strfind’ (string expected, got nil)e[0m que puedo hacer

Getting this error too. @Woopi do you have any idea what could cause this?

This error is caused by LoadResourceFile() not being able to load any json with json.decode(). You need to have some initial json in the .json file

Hey, im converting owned_vehicles table in my server’s db to json, and I want to store it in json
For now I have this:


image

I want that plate table to be named as a plate defined in function that I called
so plate table should be named dsasada

in the json loading variable you are indexing after the function call, like

local loadFile= LoadResourceFile(GetCurrentResourceName(), "./fileNameHere.json")[1]

--try this
local loadFile= LoadResourceFile(GetCurrentResourceName(), "./fileNameHere.json")

Hi, so I wanted store my esx_accesorices in json, why? cuz Im bored xd.
So far I have stored data in json but problem everytime when somebody else buys something it just replaces last saved data with new, the data does not line up
Server side code:

local niz = {}
RegisterServerEvent('maske:json')
AddEventHandler('maske:json', function(skin, accessory)
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)

	
	local itemSkin = {}
	local item1 = string.lower(accessory) .. '_1'
	local item2 = string.lower(accessory) .. '_2'
	itemSkin[item1] = skin[item1]
	itemSkin[item2] = skin[item2]

	local maskaa = { Vlasnik = xPlayer.identifier, Maska1 = itemSkin[item1], Maska2 = itemSkin[item2] }

	niz = maskaa
	SaveResourceFile("dodatci", "sacuvanemaske.json", json.encode(niz), -1)
end)

Client Trigger:

TriggerServerEvent('maske:json', skin, accessory)

How it looks in json:

{"Maska2":7,"Maska1":123,"Vlasnik":"steam:11000013772b05d"}

So its stores everything correctly but everytime somebody buys it just replaces data, no lining up.

Thanks in advance :smiley:

1 Like

Thats cause you are replacing the whole table, you have to insert that information into the niz table like this, instead of doing niz = maskaa do table.insert(niz, maskaa).
If you only want rewrite information about that players mas you would have to search through the niz table and replace required information. If you need that, let me know.

local niz = {}
RegisterServerEvent('maske:json')
AddEventHandler('maske:json', function(skin, accessory)
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)

	
	local itemSkin = {}
	local item1 = string.lower(accessory) .. '_1'
	local item2 = string.lower(accessory) .. '_2'
	itemSkin[item1] = skin[item1]
	itemSkin[item2] = skin[item2]

	local maskaa = { Vlasnik = xPlayer.identifier, Maska1 = itemSkin[item1], Maska2 = itemSkin[item2] }

	table.insert(niz, maskaa)
	SaveResourceFile("dodatci", "sacuvanemaske.json", json.encode(niz), -1)
end)
3 Likes

it still just replaces data

Are you sure you are using my edit of the code? Paste here how it looks now in your script

2 Likes