Trouble with reading/opening a json file in the editor v3.2.3 stable

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

I followed a recent YT tutorial(posted Feb 2021) on creating an inventory system in Godot and it involved reading a json file that you created. It is a global script and I have been trying to debug the error, but am at a loss for the moment. I will post the code below and the error it gives me when reading the file. I have put in a test to see if the file exists in the current directory and it does but fails at the open method. The json file was created from a spreadsheet and is in a proper json format as well. The function seems straight forward, but I am not sure if this a bug or just me implementing things improperly. The version of Godot I am using is the current stable 3.2.3.

var item_data = {}

func _ready():
var item_data_file = File.new()
if not item_data_file.file_exists(“res://assets/json/test.json”):
print(“cant find the json file”)
item_data_file.open(“res://assets/json/test.json”, File.READ)
var data_text = item_data_file.get_as_text()
var item_data_json = JSON.parse(item_data_file.get_as_text())
item_data_file.close()
item_data = item_data_json.result

Error = Invalid get Index ‘Read’ (on base:‘GDScriptNativeClass’).

This happens at the item_data_file.open(…) line.

:bust_in_silhouette: Reply From: Inces

You must have a typo in that line. Don’t copy paste code form tutorial, let editor help You with entries. It will be (underscore) _File.READ.

Take that line and start typing itemdatafile.open(“res://assets/json/test.json”,
and right after comma editor will show You options with correct syntax

Thanks, I appreciate the quick response. I was unable to get it work with a (underscore)_File.READ, but was able to get it to work by using the created var in the parameter slot, i.e. item_data_file.READ.

A interesting side note is right before I read your answer, I got the previous code to work just fine by changing the test.json filename to test.txt. Changing the corresponding file_exists and open functions to point to the new .txt name as well of course.

Both of these ways loaded the data properly into item_data {}.

Thanks,

r.bailey | 2021-02-26 22:01

:bust_in_silhouette: Reply From: exuin

I’m assuming that your code is like this when it’s properly formatted?

var item_data = {}

func _ready():
    var item_data_file = File.new()
    if not item_data_file.file_exists("res://assets/json/test.json"):
        print("cant find the json file")
    item_data_file.open("res://assets/json/test.json", File.READ)
    var data_text = item_data_file.get_as_text()
    var item_data_json = JSON.parse(item_data_file.get_as_text())
    item_data_file.close()
    item_data = item_data_json.result

The problem is, you’re not returning from the function inside the if statement, so the function will attempt to open the file regardless of whether the file exists or not.

Also, this isn’t a game-crashing bug, but you don’t actually do anything with the data_text variable.

Yes sorry about not formatting the code properly, the issue was not with the file_exists function at all. I added that to check to make sure if could properly locate the file. It was able to always find the file, the issue was with loading the json file in the open function.

Like I said initially, I wasn’t sure if it was a bug or not. I could just be using it improperly. Also you’re right it is not a game crashing bug, but if you expect a file to be loaded and some information from it to use in your game, then it is game breaking. But only if the way I was using it or suggested to use it(see first response), was intended use. I added that this function works fine in an earlier response by changing the file.json → file.txt. I also got it work by using the item_data_file.Read in place of the File.Read parameter.

Thanks for your input.

r.bailey | 2021-02-26 22:47