Arrays as a key in JSON dictionary, JSON parse problem

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

Hello community,
I have a problem with JSON parsing

I have a dictionary that I save as a line to the file.json

func save_to_file():
   save_to_slot("slot_1")

   yield(get_tree().create_timer(0.5), "timeout")
   var save_game = File.new()

   save_game.open("user://slot_1.json", File.WRITE)
   save_game.store_line(JSON.print(data2save.duplicate()))

   save_game.close()

Then I load it

func load_from_file():
   var save_game = File.new()

   if not save_game.file_exists("user://slot_1.json"):
	   print("File %s doesn't exist.","user://slot_1.json")
	   return

   save_game.open("user://slot_1.json", save_game.READ)
   var text = save_game.get_as_text()

   var data = JSON.parse(text).result

   save_game.close()

   yield(get_tree().create_timer(0.5), "timeout")
   slots["slot_1"] = data
   yield(get_tree().create_timer(0.5), "timeout")

   load_from_slot("slot_1")

data structure looks like this:
data2save = { [[map_filename, node_name], node_data] }

real data example would be:
{ “[res://level1.tscn, player1]”:{“HP”:10,“weapon”:“sword”,“bDead”:False}}

when JSON parses the save_game.get_as_text() variable the Array key becomes String

Any idea how can I convert it back to an Array key? If you want more details ask me anytime

I remember I had a similar issue. I ended up making a custom string2array() function and saved it in a global/autoload file so I could re-use it.

Basically it took a string as a parameter, then created a new array, parsed the string and stored the parse string into the new array. Then the function returned the new array.

eod | 2020-12-09 07:54