0 votes

I'm using a json file to store assets such as dialogues, sprites, and sounds. This A.P.I was built to take the data from the file and print into the output; but the out put keeps churning out null and i don't know how to fix it.

extends Node

    var data = {}
    var path = "res://Json_Files/test_file1.json"

    export (String, FILE) var json_file = null

    func _ready():
        var jsonfile = File.new()
        jsonfile.open(path, File.READ)
        data = parse_json(jsonfile.get_as_text())
        print(data)
in Engine by (41 points)

2 Answers

+1 vote

Your JSON file is probably invalid, and parse_json doesn't provide much error reporting.

First, make sure your file is actually loading properly: check if jsonfile.open(path, File.READ) returns OK.

If it does, try to use JSON.parse(jsonfile.get_as_text()) instead. This returns a JsonParseResult, which contains both the resulting data and and error information if there is a problem: https://docs.godotengine.org/en/3.0/classes/class_jsonparseresult.html#class-jsonparseresult.
You can also check the console output because errors could get reported here as well.

You can also make sure your JSON file is valid by using a validator plugin in your text editor (Notepad++ and Sublime both have plugins for that) or you can use a website such as https://jsonlint.com/?code=

by (27,829 points)
0 votes

GDscript has this method too, you can test if your json text is bad formated:

String validate_json( String json )
Checks that json is valid JSON data. Returns empty string if valid. Returns error message if not valid.

JsonParseResult can return no error in some invalid Json text:

https://github.com/godotengine/godot/issues/19611

by (329 points)
API made, how do i call the stored information
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.