Reading the JSON file returns nothing

Godot Version

Godot 4.x

Question

Reading the json:

func readJSON(json_file_path):
var file = FileAccess.open(json_file_path, FileAccess.READ)
var content = file.get_as_text()
var json = JSON.new()
var finish = json.parse_string(content)
return finish

Content of json:
{
“bpm”:120.0,
“SongScrollSpeed”:5.0,
“SongChart”: [
{“b”:1, “s”:0, “m”:0},
{“b”:1, “d”:1, “s”:1},
{“b”:1, “d”:4, “s”:2},
]}

I’ve copied your code into my own project

extends Control

var path = "res://data.json"

func readJSON(jsonFilePath):
	var file = FileAccess.open(jsonFilePath, FileAccess.READ)
	var content = file.get_as_text()
	
	var finish = JSON.parse_string(content)
	return finish
	
func _ready():
	print(readJSON(path))

When I first ran it - the function returned a null value as there were invalid characters in the json file, being the quote marks - not sure if those quote marks are present in your json file, or what, but replacing them with regular quotes seemed to fix the issue for me

Here is the corrected JSON file that I made

{
	"bpm":120.0,
	"SongScrollSpeed":5.0,
	"SongChart": [
		{"b":1, "s":0, "m":0},
		{"b":1, "d":1, "s":1},
		{"b":1, "d":4, "s":2},
	]
}

If you look closely at the two quote marks, one of them is different
image

1 Like

nope, still doesn’t work, you can ask me anything.

Are you printing out the result anywhere? Can I see your whole code file?

Node1:
extends Node2D

var bpm: float
var SongScrollSpeed: float
var SongChart: Array

func _ready() → void:
var jsonfile = get_node(‘/root/ChartLoader’).FD_Star
print(jsonfile)
bpm = jsonfile[“bpm”]
SongScrollSpeed = jsonfile[“SongScrollSpeed”]
SongChart = jsonfile[“SongChart”]

ChartLoader (Autoloaded):
extends Node2D

var bpm: float
var SongScrollSpeed: float
var SongChart: Array

func readJSON(json_file_path):
var file = FileAccess.open(json_file_path, FileAccess.READ)
var content = file.get_as_text()
var json = JSON.new()
var finish = json.parse_string(content)
return finish

func _ready() → void:
var FD_Stars = readJSON(“res://Assets/FNFFD-Stars/FD-ChartL1.json”)

The only issue I found in your above code was trying to access FD_Stars in your Node1 script. The variable is local to the _ready function in the ChartLoader autoload, so it is only ever present in the ready function.

I changed it to be defined globally in the autoload script (at the top of the script), and the logic from Node1 is working and successfully retrieving and printing the information out (see end of post)

ChartLoader.gd

extends Node2D

var bpm: float
var SongScrollSpeed: float
var SongChart: Array

var FD_Star

func readJSON(json_file_path):
	var file = FileAccess.open(json_file_path, FileAccess.READ)
	var content = file.get_as_text()
	var json = JSON.new()
	var finish = json.parse_string(content)
	return finish

func _ready() -> void:
	# Replaced your local declaration of `FD_Stars` with a global one `FD_Star`
	# so that it can be accessed by the Node1 script
	FD_Star = readJSON("res://Assets/FNFFD-Stars/FD-ChartL1.json")

Node1.gd

extends Node2D

var bpm: float
var SongScrollSpeed: float
var SongChart: Array

func _ready() -> void:
	# Editing post: just to add, you can reference any autoload scripts
	# by referring to the node name - in this case "ChartLoader", rather
	# than getting a node reference to it using the "root/ChartLoader" path
	var jsonfile = ChartLoader.FD_Star
	bpm = jsonfile["bpm"]
	SongScrollSpeed = jsonfile["SongScrollSpeed"]
	SongChart = jsonfile["SongChart"]
	
	print(jsonfile)
	print(bpm, SongScrollSpeed, SongChart)

As you can see the results are being printed

Still empty :frowning:

I really can’t think of anything else that would be going wrong, it’s working for me. Are you sure you’re running the right scene and printing the file contents before checking the output?

Are you getting any error messages? I probably should’ve asked that first

“out of bouns index ‘1’ (on base: ‘Array’)”

Could you attach a screenshot or tell me which script and line the error is on?

This current scene is Node1 (FD_ChartL1) slr

I think your SongChart array doesn’t have any elements in, try printing SongChart out on the line before and see if it has things in, then we can go from there

Prints out an empty array

Yeah so you’re trying to get the last element in there when it’s empty, so it’ll error

Can I ask what is curr_scene and where is it defined, as that is where you are getting the SongChart from?

@onready var curr_scene = get_tree().current_scene”
the current scene is that has the Node1.gd
“SongChart = curr_scene.SongChart”

Btw this function in the Node1.gd

"func _ready() → void:
# Editing post: just to add, you can reference any autoload scripts
# by referring to the node name - in this case “ChartLoader”, rather
# than getting a node reference to it using the “root/ChartLoader” path
var jsonfile = ChartLoader.FD_Star
bpm = jsonfile[“bpm”]
SongScrollSpeed = jsonfile[“SongScrollSpeed”]
SongChart = jsonfile[“SongChart”]

print(jsonfile)
print(bpm, SongScrollSpeed, SongChart)"

Doesnt print the jsonfile var, and the other variables

Could you print out the curr_scene variable and see what is says?

Hmm… never thought of that before, ill try

Vulkan: Did not create swapchain successfully. Error code: VK_ERROR_SURFACE_LOST_KHR
Godot Engine v4.2.1.stable.official.b09f793f5 - https://godotengine.org
Vulkan API 1.1.117 - Forward Mobile - Using Vulkan Device #0: ARM - Mali-G57

{ “bpm”: 120, “SongScrollSpeed”: 5, “SongChart”: [{ “b”: 1, “s”: 0, “m”: 0 },
{ “b”: 1, “d”: 1, “s”: 1 }, { “b”: 1, “d”: 4, “s”: 2 }] }
#PRINT FROM THE CHARTLOADER

FD_Stars:<Node2D#32463914196>
#PRINTED CURRENT SCENE
— Debugging process stopped —

It seems normal (the “fd_stars” node has the Node1.gd script)