Part 1 - How can I save the foillowing as a json file....
You should be able to save like this (using your example values):
var dictionary = {
"items": {
"<item_a_id>": {
"name1":"aaa",
"name2":"bbb",
"name3":"ccc",
}
}
}
var json_data = to_json(dictionary)
The only thing I am not sure about is whether you can save dictionaries within dictionaries with Godot. I think you can save dictionaries within dictionaries, but I have not tried so it may not be possible.
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....
You can save/load the JSON file in the user://
directory (see the documentation for saving games for more info).
As for updating the JSON data, it should look like this:
var dictionary = {
"items": {
"<item_a_id>": {
"name1":"aaa",
"name2":"bbb",
"name3":"ccc",
}
}
}
" Check to see if there is a save file... "
var file = File.new();
if (file_exists("user://Save_Data.json")):
file.open("user://Save_Data.json")
dictionary = parse_json(file.get_line(), File.READ)
file.close()
" Check to see if item_b_id exists "
print (dictionary["items"].has("<item_b_id>"))
" Add to the dictionary "
dictionary["items"]["<item_b_id>"] = { "name1":"ddd", "name2":"eee", "name3":"fff"}
dictionary["items"]["<item_c_id>"] = { "name1":"ggg", "name2":"hhh", "name3":"iii"}
" Save the dictionary "
file.open("user://Save_Data.json", File.WRITE)
file.store_line(to_json(dictionary))
file.close()
Hopefully this helps :)