JSON file loading crash

Hello. I am trying to access a json file here which will store the highscores for my game. This bit of code here is so I can access the file and I will make it so the high score is displayed on the title screen.

 local json = require("json")

local function loadHighscore(filename)
	local content = sys.load(filename)
	if content == nil then
		print("Failed to load JSON content from file: " .. filename)
		return nil
	end

	local success, data = pcall(json.decode, content)
	if not success or not data or not data.numbers then
		print("Invalid or missing data in JSON file: " .. filename)
		return nil
	end

	table.sort(data.numbers, function(a, b) return a > b end)

	--Return the highest score (first value)
	return data.numbers[1]
end

function init(self)
	self.file_path = sys.get_save_file("main", "test.json")
	local highscore = loadHighscore(self.file_path)
end

I have accessed the json file using the same method in another script to sort the data however when this script runs it crashes. This is what the console reads:


Why is this happening?

Is your test.json file created by sys.save() or just your plain json string file?

I’m not too sure. I think it’s done through sys.save(). Should I attatch the other script so you can see what’s happening there?

When saving and loading using sys.save(), sys.load(), you don’t need to decode it because it is already parsed to a lua table.

I have changed the script so it is not decoding but it is still crashing unfortunately

local function loadHighscore(filename)
	local content = sys.load(filename)
	if content == nil then
		print("Failed to load JSON content from file: " .. filename)
		return nil
	end

	local data = content
	if not data or not data.numbers then
		print("Invalid or missing data in JSON file: " .. filename)
		return nil
	end

	table.sort(data.numbers, function(a, b) return a > b end)

	-- Return the highest score (first value)
	return data.numbers[1]
end

function init(self)
	self.file_path = sys.get_save_file("main", "test.json")
	local highscore = loadHighscore(self.file_path)
end

I think your file is not created by sys.save(). Can you show me where you save it?

Like the file address or where I save it in the script?

Where in your code

Not sure if this exactly what you meant but here is code where stuff is written to the file.

local function writeJSONToFile(file_path, data)
	local file = io.open(file_path, "w")
	if not file then
		print("Failed to open JSON file for writing")
		return
	end
	file:write(json.encode(data))
	file:close()
end

Generally, the sys.save() will save a lua table to file and sys.load() will load that file to a lua table. You don’t need to encode or decode your data

You just need to use sys.save in that place, do not encode your table. Or if you use io. file write then use io. file:read then

1 Like

Like this?

local function saveDataToFile(file_path, data)
	local success = sys.save(file_path, data)
	if not success then
		print("Failed to save data to file: " .. file_path)
	end
end
1 Like

I still get the same crash

You can go to the saved file location to check. I think your old file is still there. If yes, delete it and try again. Also make sure the file path is correct

1 Like

We’d like to look into this! Can you please create a ticket on GitHub and share a minimal project where the crash happens.

1 Like

I can’t get the crash to trigger again unfortunately and I can’t figure out why, can I zip up my project and put it here?

Oh, ok, if it is not crashing anymore there’s probably not much point to share the project. Let us know if it crashes.

No I mean my main project is crashing still but in the new minimal project I cannot replicate the crash. Sorry for the confusion.

Ok, are you able to share it (you can share it with me, bjorn@defold.se)

I have emailed you it now.