Skip to main content
TRACK B·DATA AND PERSISTENCE·Verified June 2026 · Lua 5.4 · ox_lib 3.x
Learning with an AI assistant?
Copies this lesson plus 2026 ground rules (no lua54 'yes', Cfx.re Portal, correct callback signatures) as a ready-to-paste mentor prompt.

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.

You'll build
A resource that saves and loads player data using JSON - name, money, and job - and survives server restarts.
Time
~20 minutes
You need
A local FiveM server, one resource folder, basic Lua knowledge.
You'll learn
What JSON is, json.encode() and json.decode(), SaveResourceFile and LoadResourceFile, real multi-player patterns, and when JSON beats a database.
BEFORE YOU START

Build it

Create the resource

A resource folder with a server script.
code
resources/qu_json_demo/
fxmanifest.lua
server.lua
code
fx_version 'cerulean'
game 'gta5'
server_script 'server.lua'

Save data to JSON

A .json file appears with your data inside.
code
-- 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() - returns qu_json_demo, so the file lives inside this resource
  • SaveResourceFile() - writes content to a file on disk. Only works server-side
  • json.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

You can read the saved data back into Lua.
code
-- 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. Returns nil if the file doesn't exist
  • json.decode() - converts JSON text back to a Lua table. After this, data.name works like any table field

Update and re-save data

The file reflects your changes.

Loading, editing, and saving is the full loop:

code
-- 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

Each player has their own data, keyed by license.

Real servers store data per-player using identifiers as keys:

code
-- 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:

code
{
"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.

Still ahead in this lesson
  • JSON vs databases - when to use which
  • Common failures

The remainder of Using JSON in FiveM is available to FiveM School members.