Need help with json file saving?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BigDC
I use Godot 3.0.6.
I have a 2-Part question..
Part 1 - How can I save the foillowing as a json file....
-All the keys and values here are just examples-

    {
     "Items": {
       "<item_a_id>": {
         "name1": "aaa",
         "name2": "bbb",
         "name3": "ccc"
        }
      }
    }
    
    
    Part 2 -Then how can I update and SAVE the ABOVE json file so it can have things added to it and will start too look like this....
    -All the keys and values here are just examples-
    
    {
     "Items": {
       "<item_a_id>": {
          "name1": "aaa",
          "name2": "bbb",
          "name3": "ccc"
         },
    
       "<item_b_id>": {
          "name1": "ddd",
          "name2": "eee",
          "name3": "fff"
        },
    
       "<item_c_id>": {
          "name1": "ggg",
          "name2": "hhh",
          "name3": "iii"
        },
      }
    }
:bust_in_silhouette: Reply From: originaltenka

Link to the docs: Godot Docs

This question could have been avoided entirely
For your first question:

func save_game():
var save_game = File.new()
save_game.open(“user://savegame.save”, File.WRITE)
var save_nodes = get_tree().get_nodes_in_group(“Persist”)
for i in save_nodes:
var node_data = i.call(“save”);
save_game.store_line(to_json(node_data))
save_game.close()

For your Second question:
The easiest method would be to load the saved json file and edit, then save again…

func load_game():
var save_game = File.new()
if not save_game.file_exists(“user://save_game.save”):
return # Error! We don’t have a save to load.
# We need to revert the game state so we’re not cloning objects during loading. This will vary wildly depending on the needs of a project, so take care with this step.
# For our example, we will accomplish this by deleting savable objects.
var save_nodes = get_tree().get_nodes_in_group(“Persist”)
for i in save_nodes:
i.queue_free()
# Load the file line by line and process that dictionary to restore the object it represents
save_game.open(“user://savegame.save”, File.READ)
while not save_game.eof_reached():
var current_line = parse_json(save_game.get_line())
# First we need to create the object and add it to the tree and set its position.
var new_object = load(current_line[“filename”]).instance()
get_node(current_line[“parent”]).add_child(new_object)
new_object.position = Vector2(current_line[“pos_x”], current_line[“pos_y”]))
# Now we set the remaining variables.
for i in current_line.keys():
if i == “filename” or i == “parent” or i == “pos_x” or i == “pos_y”:
continue
new_object.set(i, current_line[i])
save_game.close()