Json file read; but output says null

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

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

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: JSONParseResult — Godot Engine (3.0) documentation in English.
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 JSON Online Validator and Formatter - JSON Lint

:bust_in_silhouette: Reply From: 807

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: