How to parse a JSON file I wrote myself.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DriNeo
:warning: Old Version Published before Godot 3 was released.

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

:bust_in_silhouette: Reply From: DriNeo

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.

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.

DriNeo | 2016-09-26 18:12

try res:// instead of user://

codevanya | 2017-07-28 09:13

:bust_in_silhouette: Reply From: rafeu

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: Loading...

How do I get the json file imported?

Allen Kennedy Jr. | 2020-05-18 19:18

:bust_in_silhouette: Reply From: AlvaroAV

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)

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.

Crease | 2019-04-03 06:56

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

frankiezafe | 2019-06-03 17:51

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.

Allen Kennedy Jr. | 2020-05-18 19:18

:bust_in_silhouette: Reply From: demwilson

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")
:bust_in_silhouette: Reply From: jeroenheijmans

Godot 4.x

The File stuff has been changed over to work via FileAccess so things seem to have changed a little. Here’s what worked for me in Godot 4:

func readStageJson():
	var fileName = "res://game/stage_data.json"
	var file = FileAccess.open(fileName, FileAccess.READ)
	var contents = file.get_as_text()
	file.close()
	return JSON.parse_string(contents)

See official FileAccess docs for more info.