Unrecognized JSON parse error

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bluecatgames

Hi Guys
i’m getting an unrecognized error while using json files.

Error message:

E 0:00:05.602   parse: Error parsing JSON at line 37: Expected 'EOF'

Here is my original json file: https://pastebin.com/8vBaJpeY
Was verified using jsonlint.com and is OK.

And here is my code:

func updateDB(pos,item,new_item_id = null):
if new_item_id != int(item.name.left(5)):
	var equipSlotData = {}
	var equipSlotData_file = File.new()
	equipSlotData_file.open("res://DBFiles/EquipSlotDB.json",File.READ_WRITE)
	var equipSlotData_file_json = JSON.parse(equipSlotData_file.get_as_text())
	equipSlotData = equipSlotData_file_json.result
	
	var slot = get_slot_under_pos(pos)
	if new_item_id == 0:
		equipSlotData[prev_slot.name]["ItemID"] = new_item_id
	else:
		equipSlotData[slot.name]["ItemID"] = int(item.name.left(5))
		prev_slot = slot.name
	equipSlotData_file.store_string(JSON.print(equipSlotData, "\t"))
	equipSlotData_file.close()
	EquipSlotDB.update_equipslot_db()

The error seems to happen in that part:

if new_item_id == 0:
		equipSlotData[prev_slot.name]["ItemID"] = new_item_id

Before that code condition GODOT changes my json format to a invalid format inserting two ‘}’ in the end of my json file. (see below)
Here the corrupted json file with two ‘}’ inserted by godot
CorruptedJSON

I dont know why this happen, can you plz help me?

Thx!

try saving JSON file as .tres

Inces | 2022-09-20 09:44

saved as tres and the same error continue happen

bluecatgames | 2022-09-20 10:52

I don’t know about it than.
In earlier version I was using JSON for my project in similar manner, I created it with google sheet and Export Sheet Data extension. It worked, maybe try that too

Inces | 2022-09-20 11:47

yeah i made the same process, created the json using an extensions in google sheets. The curious thing is, when i update my inventory grids data using almost the same code that runs well, see:

func updateDB(item):
if slotUsed != item_last_pos:
	var inv_data = {}
	var inv_data_file = File.new()
	inv_data_file.open("res://DBFiles/InvDB.json",File.READ_WRITE)
	var inv_data_file_json = JSON.parse(inv_data_file.get_as_text())
	inv_data = inv_data_file_json.result
	if item_last_pos != null:
		inv_data[item_last_pos]["ItemID"] = 0
	inv_data[slotUsed]["ItemID"] = int(item.name.left(5))
	inv_data_file.store_string(JSON.print(inv_data, "\t"))
	inv_data_file.close()
	InvDB.update_inv_db()

That code runs well, and the json structure is the same, only differe in the keys names.

Has a chance to this be a bug in Godot?

bluecatgames | 2022-09-20 12:23

If it works on one dictionary I don’t think it is a bug.
error is about parsing, so it breaks on loading. Did You try to use it with File.READ only ?

Inces | 2022-09-20 13:01

I think i solved the problem.
Changed my update function to:

func updateDB(pos,item,new_item_id = null):
EquipSlotDB.update_equipslot_db()
var equipSlotData = EquipSlotDB.equip_slot_data
var slot = get_slot_under_pos(pos)
var equipSlotData_file = File.new()
equipSlotData_file.open("res://DBFiles/EquipSlotDB.json",File.WRITE)

if new_item_id != null:
	equipSlotData[prev_slot.name]["ItemID"] = int(new_item_id)
	print(equipSlotData)
else:
		equipSlotData[slot.name]["ItemID"] = int(item.name.left(5))
equipSlotData_file.store_string(JSON.print(equipSlotData, "\t"))
equipSlotData_file.close()
EquipSlotDB.update_equipslot_db()

And now runs without erros, thx for help Inces.

bluecatgames | 2022-09-20 17:03

Can You explain what did You change and why it worked ?

Inces | 2022-09-20 19:18

I think the error occurs because in the first script i’m using READ_WRITE and in the second one i changed to only WRITE. And to read the updated data i only runs again the function update_equipslot_db in the Singleton to update the equip_slot_data dict with the required data.

Probably READ_WRITE was not the better choice for my project.

bluecatgames | 2022-09-20 20:18

cool, thanks also :slight_smile:

Inces | 2022-09-20 20:33

:bust_in_silhouette: Reply From: bluecatgames

Solved in the comments.