Exported project can't read JSON file despite adding *.json to export filters

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

My game runs perfectly fine when run in the editor, but the exported version can’t read the JSON file.

The relevant code:

func load_dict(name):
  var dict := {}
  var fp = "res://maps/%s.json" % name
  dir_contents("res://maps")
  var file = File.new()
  if not file.file_exists(fp):
	print("File not found: ", fp )
  else:
	print('File found: ', fp)
  file.open(fp, file.READ)
  var text = file.get_as_text()
  dict = parse_json(text)
  file.close()

	  return dict
	
func dir_contents(path):
	var dir = Directory.new()
	if dir.open(path) == OK:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "":
			if dir.current_is_dir():
				print("Found directory: " + file_name)
			else:
				print("Found file: " + file_name)
			file_name = dir.get_next()
	else:
		print("An error occurred when trying to access the path.")

export settings

Export filters:

*.json

error message

Found file: phandalin.json.import
File not found: res://maps/phandalin.json
ERROR: File must be opened before use.
   At: core/bind/core_bind.cpp:2126
ERROR: call: Error parsing JSON at line 0:
   At: modules/gdscript/gdscript_functions.cpp:1268
SCRIPT ERROR: load_dict: Trying to assign value of type 'Nil' to a variable of type 'Dictionary'.
          At: res://scripts/world.gdc:175

No such errors appear when running the game from the editor.

The function dir_contents is for debugging purposes and isn’t used anywhere else.

The line res://scripts/world.gdc:175 refers to dict = parse_json(text)

Any help would be very appreciated. Thank you in advance!

I’m especially confused that the .json.import is exported but the original .json is not.

NickFegley | 2021-08-13 00:29

I believe you have to read res://maps/%s.json.import (I had the same problem once).
I’m not sure I understand why though…

Edit: you have to use the ResourceLoader, see the relevant doc (litterally the first part of the page)!

Lola | 2021-08-15 15:54