learning how to handle Json files as Data in Godot

Godot Version

4.2.2

Question

i’m trying to learn how to use and handle json file as datafile and i’m struggling to find out how to properly set up my GameData script to load differents json file and for example use statsitemdata to fill the central main itemdata that are parsed in dictionary i’ve tried some things but it seems to not be working properly if you have any informations , advices or anythings to say feel free i’m open to everything i’m new on godot 4.2 and trying to learn

here is my gamedata script if needed : `extends Node

var content : Dictionary
var item_Data = {}
var loot_Data = {}
var ItemequipmentSlots_Data = {}
var item_data_file_path = “res://Singletons/itemData.json”
var itemEquipmentSlots_data_file_path = “res://Singletons/itemEquipmentSlots.json”
var monster_data_file_path = “res://Singletons/MonsterTable.json”
var Monster_Data = {}
var gold = 100000

func get_texture_name (ID = “0”):
return content[ID][“Texture”]

func get_Item_Name (ID = “0”):
return content[ID][“Name”]

func get_Item_equipableSlot (ID=“0”):
return content[ID][“equipableSlot”]

func get_Monster_Name (ID = “0”):
return content[ID][“Name”]

func _ready():
item_Data = load_json_file(item_data_file_path)
print(“item_Data has been loaded”)
print("item_Data: ", item_Data)
itemEquipmentSlots_data_file_path = load_json_file(itemEquipmentSlots_data_file_path)
print(“itemEquipmentSlots_data has been loaded”)
print("itemEquipmentSlots_data: ", ItemequipmentSlots_Data)
Monster_Data = load_json_file(monster_data_file_path)
print("monster data has been loaded Monster_Data: ", Monster_Data)

func load_json_file(filePath : String):
if FileAccess.file_exists(filePath):
print(“FileAcess to filepath correct”)
var dataFile = FileAccess.open(filePath, FileAccess.READ)
var parsedResult = JSON.parse_string(dataFile.get_as_text())
print(“Json.parse datafile : dataFile”)

	if parsedResult is Dictionary:
		print("Parsedresulted in Dictionary")
		return parsedResult
	else:
		dataFile.close()
		print("error reading files datafile closed")
if item_Data["equipableSlot"] == "to_be_filled_by_GameDataScript":
	print("Equipableslot to be filled by gamedatascript")
	item_Data["equipableSlot"] = ItemequipmentSlots_Data[item_Data["id"]]
	print(item_Data)

to easily read what’s inside a JSON, just use this

@export_file("*.json") var leveling_file
...
var json_as_text = FileAccess.get_file_as_string(leveling_file)
var leveling_data = JSON.parse_string(json_as_text)

the leveling_data should be in Dictionary type now, depending on how your data in JSON is structured, type may vary. because parse_string returns Variant type

1 Like

thanks alot for the quick help , i might have explained badly myself about my problem

you gave me a script that load json and parse it but seems way more simple that the one i’m using

i would like to load all my json file parse them and then read them to after write empty colum who are suposed to be fill by other json files
here is some screenshot so it might help about what i’m trying to do


in this screenshot i’ve opened gamedata while runing to see if the "to_be_filled_by_GameDataScript" has been replaced by the correct value from other json itemEquipableSlots but it seems to not working with my if : ```
if item_Data[“equipableSlot”] == “to_be_filled_by_GameDataScript”:
print(“Equipableslot to be filled by gamedatascript”)
item_Data[“equipableSlot”] = ItemequipmentSlots_Data[item_Data[“id”]]
print(item_Data)


i would like to take this “one handed” id 1 to relate and write over item data in the “to be filled by GameDataScript”(on the first screen) to resume i think i want to get the value of the key in itemData that concern the slots and use the one in itemslotdata to replace its value in itemData

update after some research and learning i’ve found a part of my problem how to locate and call for key i found out this :
var itemSlots = item_Data[“itemID”][“1”][“itemSlots”]
print (“itemslots:”, itemSlots)
who give me the correct answer now i still have to figure out how to change this value from this dictionary from another dictionary relating on the ID number