0 votes

I have a json file which looks like this:

{
    "word_1" : {
         "en" : "engine"
         "de" : "motor"
     }
    "word_2" : {
         "en" : "cookie"
         "de" : "keks"
     }
}

In my script I have

var words = {
    "word_1_en" : "",
    "word_1_de" : "",
    "word_2_en" : "",
    "word_2_de" : ""
}

Now how do I get the word1en set from the json file {"word_1":{"en":""}}
And the same of course for the other values.

Godot version 3.3.2
in Engine by (12 points)

1 Answer

+2 votes

Hello,
to turn the content of a JSON file into a godot variable you can use the global parse_json function:

func read_json_file(file_path):
    var file = File.new()
    file.open(file_path, File.READ)
    var content_as_text = file.get_as_text()
    var content_as_dictionary = parse_json(content_as_text)
    return content_as_dictionary

For your problem I would use the same data structure in the JSON file and in the dictionnary, that is to say formatting the JSON file this way so that there is no conversion needed:

{
    "word_1_en" : ""
    "word_1_de" : ""
    "word_2_en" : ""
    "word_2_de" : ""
}

Finally, it looks like your a trying to use a custom solution to handle translations.
In case you didn't know, godot has tools to support that, see the relevant docs.

by (734 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.