This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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!

Godot version 3.5 stable
in Engine by (21 points)
edited by

try saving JSON file as .tres

saved as tres and the same error continue happen

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

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?

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 ?

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.

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

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

Probably READ_WRITE was not the better choice for my project.

cool, thanks also :)

1 Answer

0 votes
Best answer

Solved in the comments.

by (21 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.