Working with JSON Files¶
Guide for reading and writing JSON files in FiveM resources.
Reading from JSON File¶
Load and parse a JSON file from your resource:
local loadFile = LoadResourceFile(GetCurrentResourceName(), "./data/fileName.json")
local extract = {}
extract = json.decode(loadFile)
After reading, you can modify the data and save it back:
Writing to JSON File¶
Create or overwrite a JSON file with new data:
local information = {
name = "w00pi the legend",
age = 17,
coolness = 1000
}
SaveResourceFile(GetCurrentResourceName(), "data/fileName.json", json.encode(information), -1)
FXManifest Configuration¶
Don't forget to declare your JSON files in fxmanifest.lua:
This ensures the files are included when the resource is loaded and streamed to clients if needed.
Best Practices¶
- Error Handling: Always check if
LoadResourceFilereturnsnilbefore decoding - Pretty Print: Use
json.encode(data, {indent = true})for readable output - Path Separator: Use forward slashes
/in paths, even on Windows - File Location: Store data files in a dedicated
data/folder for organization