+6 votes

I want to write dialogues in a json file and pull the datas to be displayed in the game. I read the docs but it talks about saving data from the nodes to json. I want to parse a json created by myself and I'm totally lost. I'm sure it's a common task.
regards

in Engine by (516 points)
edited by

4 Answers

+3 votes

Ok, on Windows " user:// " is in the appdata folder.

If I write the json in that folder The following code works.

var dict = {}

func _ready():
 var file = File.new()
 file.open("user://panelText.json", file.READ)
 var text = file.get_as_text()
 dict.parse_json(text)
 file.close()
# print something from the dictionnary for testing.
 print(dict["text_1"])

I wonder if it's the right way to do this kind of stuff.

by (516 points)

After a test it's not a good solution because if I run this app on another PC, or another windows user account, the app doesn't found the text anymore, and the label is empty. I need to bundle the json into the app.

try res:// instead of user://

+5 votes

Just bringing your solution with the question

var dict = {}

func _ready():
  var file = File.new()
  file.open("res://Ress/panelTextn2.json", file.READ)
  var text = file.get_as_text()
  dict.parse_json(text)
  file.close()
  get_node("Area/Panel/Label").set_text(dict["text_1"])

And remember to go to Export, then the resources tab and set the export mode to Export all resources in the project to make godot include the JSON file in the build

Answer from: https://godotdevelopers.org/forum/discussion/17449/how-to-manage-texts

by (49 points)

How do I get the json file imported?

+14 votes

For Godot 3 the answer is:

var text_json = "{\"error\": false, \"data\": {\"player_id\": 1}}"
var result_json = JSON.parse(text_json)
var result = {}

if result_json.error == OK:  # If parse OK
    var data = result_json.result
    print(data)
else:  # If parse has errors
    print("Error: ", result_json.error)
    print("Error Line: ", result_json.error_line)
    print("Error String: ", result_json.error_string)
by (84 points)

After years of neglecting my degree in favor of doing other stuff, it's posts like this that make me excited to develop using Godot.

I quit playing video games so I could make one. Just registered on this forum to learn as much as I can.

Thanks for all your hard work and knowledge.

great! it worked 10 sec after the copy/paste!

This does not answer the question, "How to parse a JSON file I wrote myself?"

This instead answers the question, "How to parse a JSON STRING I wrote myself?"

Nice answer... wrong question.

+1 vote

If you piece several of the existing answers together, you get a workable answer for this question.
NOTE: Using Godot 3.2
Actual function that loads JSON file:

func load_json_file(path):
    """Loads a JSON file from the given res path and return the loaded JSON object."""
    var file = File.new()
    file.open(path, file.READ)
    var text = file.get_as_text()
    var result_json = JSON.parse(text)
    if result_json.error != OK:
        print("[load_json_file] Error loading JSON file '" + str(path) + "'.")
        print("\tError: ", result_json.error)
        print("\tError Line: ", result_json.error_line)
        print("\tError String: ", result_json.error_string)
        return null
    var obj = result_json.result
    return obj

Calling that function from a scene script:

var loaded_object = load_json_file("res://game/json/abilities.json")
by (58 points)
edited by
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 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 webmaster@godotengine.org with your username.