Dictionaries - adding content

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

Hi, I’m new to Godot and I’ve been thinking… is it possible to make a json file, then make a script that adds/edit content in it?

I’m asking this because I had this idea to make a project manager desktop program in Godot, I feel way more motivated than doing this on Excel VBA, plus it would make me practice more in this engine.

I don’t know yet how am I gonna do it, but just a draft of it is something like this:

 
 "<Insert Goal #1 Here>" :
        {
            "<Insert Year Here>" :
            {
                "<Insert Month Here?>" : 
                 {
                    "Task #1": "", "Task Description: "", "Task Deadline": "", "Task Journal": "
                 }
            }
        }

Would it be possible to add more goals, years and tasks in the json file through the software, or would I have to make a lot of editable data (e.g. goal #200, task #50, etc) and edit them?

It will be for personal use, so I don’t mind if it sucks :smiley: I just wanna know if it’s possible and if there are better alternatives, Idk.

Thanks.

:bust_in_silhouette: Reply From: Zylann

It is possible to edit content with Godot:

Now unless you plan to do batch operations on this json file, I don’t see a practical reason to build an entire editor rather than just hand-edit it, but that’s your choice :slight_smile:

Would it be possible to add more goals, years and tasks in the json file through the software, or would I have to make a lot of editable data (e.g. goal #200, task #50, etc) and edit them?

I don’t understand this question. It litterally says “Is it possible to add more goals, or would I have to add more goals”… yes? I mean, you can add more goals as long as they have unique identifier within the dictionary. If you don’t want that restriction, perhaps you could use an array instead.

I’ve been researching about it, I couldn’t quite understand the docs, but I’ve found this link, it was answered by you too.

So I’ve tried it:

onready var example = $ExamplePopup

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

var names = { 
					'names' : [],
					'last_names' : []
	}
	
# Construct a dictionary with whatever data you want
func save_file():
	# Open a file
	var file = File.new()
	if file.open(SAVE_PATH, File.WRITE) != 0: # By the way I didn't understand why ' != 0 '
	    print("Error opening file")
	    return
	
	# Save the dictionary as JSON (or whatever you want, JSON is convenient here because it's built-in)
	file.store_string(names.to_json())
	file.close()

Turns out that when I try this, Godot (3.1) says "Invalid call. Nonexistent function ‘to_json’ in base ‘Dictionary’. What am I missing here? I’ve been stuck in this problem for hours with no progress. I guess that’s the only thing left to finish my project.

I just wanna save it on json so in the end it could have stored data in the file, like this:

'names':  ["Albert", "Mark", "John"]
'last_names": ["Einstein", "Zuckerberg", "Travolta"]
'professions': ["Physicist","Entrepreneur","Actor"]

The last key, for example, meaning I could add other data later (types accepted by json).

FT_Anx | 2019-09-11 04:50