Adding dictionary to json file, but then getting replaced

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

So I’m basically trying to keep adding data into the ‘test.json’ file. The program isn’t ready yet, so I play the scene every time to see if it’s working and adding data. It does add, but then it replaces the content saved every time I restart playing it. How can I solve this?

var infos = []
var title
var description

const SAVE_PATH = "user://test.json"

func save_file_test():
	# Load
	load_goals()
	
	# Modify	
	var store_data ={ 'title': title, 'description': description }
	
	# Save
	var f = File.new()
	f.open(SAVE_PATH, File.WRITE)
	if( load_goals() == null ):
		infos.append(store_data)
		f.store_string(JSON.print(infos, "  ", true))
	else:
		f.seek_end() #Idk if this has any utility here, tbh.
		var new_data = load_goals().append(store_data)
		f.store_string(JSON.print(new_data, "  ", true))
	f.close()

func load_goals():
	var f = File.new()
	f.open(SAVE_PATH, File.READ)
	var json = JSON.parse(f.get_as_text())
	f.close()
	var data = json.result
	return data
:bust_in_silhouette: Reply From: FT_Anx

I think I finally got it:

func save_file_test2():
    # Load
    var goals = load_goals()
    
    # Modify    
    var store_data ={ 'title': title, 'description': description }
    
    # Save
    var f = File.new()
    f.open(SAVE_PATH, File.WRITE)
    if( goals == null ):
        infos.append(store_data)
        f.store_string(JSON.print(infos, "  ", true))
    else:
        goals.append(store_data)
        var new_data = goals
        f.store_string(JSON.print(new_data, "  ", true))
    f.close()

I guess the problem was that this function was opening the json file multiple times and for some reason it wouldn’t add to the dictionary.