How i read and show a content of a json file in godot 4

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

i try to apply a dialogue system in godot 4, that system was built in godot 3, i’m a begginer on godot, and don’t have any idea of how fix it, and don’ find nothing about yet.

export (String, FILE, "*.json") var d_file

func load_dialogue():
var file = File.new()
if file.file_exists(d_file):
	file.open(d_file, file.READ)
	return parse_json(file.get_as_text())

i write this code in godot 3.5 after, thinking that, exporting this code from godot 3 to 4, the godot correction to new version, could solve this, but even changing the code like this:

@export  var d_file:JSON



func load_dialogue():
var file = File.new()
if file.file_exists(d_file):
	file.open(d_file, file.READ)
	var test_json_conv = JSON.new()
	test_json_conv.parse(file.get_as_text())
	return test_json_conv.get_data()

the code upward is in godot 4, which is the really tha don’t works, i tried FileAcess, FileDialog, but just problems come.

:bust_in_silhouette: Reply From: jgodfrey

This will get you started.

var file = "c:/path/to/your/file.json"
var json_as_text = FileAccess.get_file_as_string(file)
var json_as_dict = JSON.parse_string(json_as_text)
if json_as_dict:
    print(json_as_dict)

In the above, json_as_text will be the raw json data from the file and json_as_dict will be the json converted to an internal godot data structure (most likely a Dictionary) depending on the json content.

2 Likes