How do I read JSON files

Godot Version



Oki. Following problem.
I am trying to create a small dialogue system. Therefor I started watching a YouTube Tutorial on how to, but the problem is, it uses following piece of code:

var file =

But my Godot gives me following error message: Identifier "File" not declared in the current scope

So started googling and reading the docs and I found the FileAccess class.

Here is my code:

extends Control

var FILE_PATH = "res://dialogues/intro.json"

func _ready():
	var json =
	var file =, FileAccess.READ)
	var json_text = JSON.stringify(file.get_as_text(), "\t")

	var error = json.parse(json_text)
	if error == OK:
		var data_received =
		if typeof(data_received) == TYPE_ARRAY:
			print("Unexpected Data")
		print("JSON Prase Error: ", json.get_error_message(), " in ", json_text)

And here is the json file:

        "name": "test",
        "text": "This is a test message"

normaly I would expect the json to be printed (According to the JSON Class documentation.

But instead I am getting the Unexpected Data String.

Where is the problem, and am I able to solve it?

Thanks in advance!

JSON.stringify is a method used to create json files, not to read them, thats what JSON.parse is for.

A quick fix to your problem is to change the line where you assign json_text to this:

var json_text = file.get_as_text()

Also the reason the Youtube tutorial didn’t work is because it most likely uses godot3, since in godot3 the “File” class was used for file operations instead of the new “FileAccess” class

1 Like

Ohh my god you are right. Thanks. I seem to have overlooked this in the docs.

Thanks for the help!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.