Using JSON in FiveM
JSON is how simple data survives a server restart without setting up a database. Many scripts use it for config files, settings, whitelists, and temporary storage. This lesson teaches you to save, load, and update JSON files - the lightweight way to give your resource a memory.
Build it
Create the resource
resources/qu_json_demo/
fxmanifest.lua
server.lua
fx_version 'cerulean'
game 'gta5'
server_script 'server.lua'
Save data to JSON
-- Create a Lua table with player data
local playerData = {
name = "John",
money = 5000,
job = "police"
}
-- Convert to JSON and save to a file
SaveResourceFile(
GetCurrentResourceName(), -- which resource owns this file
"playerdata.json", -- the filename
json.encode(playerData), -- convert table → JSON text
-1 -- length: -1 means "all of it"
)
print("Data saved to playerdata.json")
Line by line:
GetCurrentResourceName()- returnsqu_json_demo, so the file lives inside this resourceSaveResourceFile()- writes content to a file on disk. Only works server-sidejson.encode()- converts a Lua table to JSON text. Tables become{}, strings get quotes, numbers stay numbers-1- tells FiveM to figure out the length automatically
Load data from JSON
-- Load the file contents
local file = LoadResourceFile(
GetCurrentResourceName(),
"playerdata.json"
)
-- Convert JSON text → Lua table
local data = json.decode(file)
-- Use the data like any Lua table
print("Name: " .. data.name)
print("Money: " .. data.money)
print("Job: " .. data.job)
LoadResourceFile()- reads a file's content as a string. Returnsnilif the file doesn't existjson.decode()- converts JSON text back to a Lua table. After this,data.nameworks like any table field
Update and re-save data
Loading, editing, and saving is the full loop:
-- 1. Load existing data
local file = LoadResourceFile(GetCurrentResourceName(), "playerdata.json")
local data = json.decode(file)
-- 2. Edit the values
data.money = data.money + 500 -- give the player 500 more
data.job = "detective" -- promote them
-- 3. Save back to the file
SaveResourceFile(
GetCurrentResourceName(),
"playerdata.json",
json.encode(data),
-1
)
print("Updated: money=" .. data.money .. " job=" .. data.job)
JSON files are fully rewritten every time you save - you can't edit one field in place. Load the whole file, change what you need, save the whole file.
Store data for multiple players
Real servers store data per-player using identifiers as keys:
-- Register a command so players can save their data
RegisterCommand('savemydata', function(source)
local license = GetPlayerIdentifierByType(source, 'license')
-- Load existing data (or start fresh)
local file = LoadResourceFile(GetCurrentResourceName(), "players.json")
local allData = file and json.decode(file) or {}
-- Write this player's data
allData[license] = {
name = GetPlayerName(source),
money = 5000,
job = "unemployed"
}
-- Save everything back
SaveResourceFile(GetCurrentResourceName(), "players.json", json.encode(allData), -1)
print(GetPlayerName(source) .. " data saved")
end, false)
The resulting JSON looks like:
{
"license:abc123": { "name": "John", "money": 5000, "job": "police" },
"license:def456": { "name": "Jane", "money": 2500, "job": "medic" }
}
Each license is a unique key, so data never mixes between players.
Keep reading the full lesson
Sign in to start, then unlock every step of this lesson and the full FiveM School with a membership.
- JSON vs databases - when to use which
- Common failures
The remainder of Using JSON in FiveM is available to FiveM School members.