How do I load a dictionary from a text file

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

I have a text file which has something like
a:this
b:that
c:dog

and i want to load it into godot as a dictionary

func load_file():
     var dictionary = {}
     var file = File.new()
     file.open("filelocation", File.read)enter code here
     dictionary = file.get_as_text().split(whenver there's a line break split)
    file.close

I have something like this (im on my phone so it’s not exact), but I only get an array and not a dictionary

:bust_in_silhouette: Reply From: rakkarage

you can read a dictionary using the json file functions it has a specific format tho, which you can see by calling to_json on the dictionary

extends Control

var _data := {
	"a": "this",
	"b": "that",
	"c": "other"
}

func _ready():
	var json := to_json(_data)
	print(json) # read this from file and then parse it
	var dictionary : Dictionary = JSON.parse(json).result
	for key in dictionary:
		print(dictionary[key])