How to access a file

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

I have the following code:

func Return(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray):
var status = JSON.parse_string(body.get_string_from_utf8())
var thia = JSON.parse_string()
var stageDict = {1: status}
#download_file(StageURL, path)
var Stage = stageDict[1]["status"]["eskom"]["stage"]
emit_signal("StageUpdate", Stage)
print("current = ", Stage)
print("shcedule: \n", status)

and also This:

func download_file(StageURL, path):
$Download.request(StageURL, headers)
print("file Downloaded")

as it is currently I take the data(body) returned from the http request and put it in a dictionary then access the data I want from that dictionary to print it to a label

but I have downloaded that data into a .json file is there a way to then read the .json file and put it’s contents in a dictionary

I’ve looked around and tried using:

var file ="res://path to file", File.READ)

but in godot 4 it says that identifier File not declared in current scope
how do I access the .json file and read it’s data into a dictionary?

The answer I was looking for was provided by jgodfrey while Wakatta’s answer is also correct and I was wanting to know that I wish I could mark you both as the best answer

Cyber-Kun | 2023-03-06 18:59

:bust_in_silhouette: Reply From: Wakatta

Your Question is kinda ironic as File as been changed to FileAccess"res://path to file", FileAccess.READ)
:bust_in_silhouette: Reply From: jgodfrey

To the broader question of how to read / parse JSON from a file, here’s a simple example with little error checking:

var file = "c:/path/to/your/file.json"
var json_as_text = FileAccess.get_file_as_string(file)
var json_as_dict = JSON.parse_string(json_as_text)
if json_as_dict:

Do know that parse() or parse_string() will return an appropriate Godot data type based on the contents of the JSON file (while that’s typically a Dictionary, it could also be an Array for example). You can verify the type of the data returned via a typeof() check.

I believe it’s a string but I’m gonna put it in a dictionary anyway so thet I can serch through it for the information I want using keys

Cyber-Kun | 2023-03-06 18:01

if it’s already a dictionary that’d be great but i know for a fact that if i put it in a dictionary like I did in the above code of the post it works to: var get Stage = stageDict[1][comment1-“status”][“eskom”][comment1-“stage”]

after putting the http request’s body in a dictionary so I’m sure it will work if I read the file contents and put it in a dictionary as well. since I don’t know if you can search for a specific part of the json information in the file this would be my solution to that

Cyber-Kun | 2023-03-06 18:05

I’m not sure I understand your above comments. And, maybe you’re making this more complicated than necessary.

My above example demonstrates how to take JSON data, stored in a file and load it into an appropriate Godot-based data-structure. If your JSON data is typical (key/value pairs), the data structure returned by either parse method will be a Dictionary.

Ultimately, you don’t control that directly. The engine decides what structure to return from the parse() call based on the contents of the JSON.

Again, if your data is key/value pairs, it’ll come base as a Dictionary. There’s a bit more information on the topic in the Dictionary docs page. Specifically, this bit:

parse is used to convert any existing JSON data into a Variant that can be used within Godot. If successfully parsed, use data to retrieve the Variant, and use typeof to check if the Variant’s type is what you expect. JSON Objects are converted into a Dictionary, but JSON data can be used to store Arrays, numbers, Strings and even just a boolean.

Also, note that parse() and parse_string() do the same basic thing. parse() is just the non-static version of parse_string() and offers a more detailed level of error checking…

jgodfrey | 2023-03-06 18:15

If I could kiss youI would Your solution worked perfectly!! thank you so much I’ve been struggling with this for a week!!

Cyber-Kun | 2023-03-06 18:47

yes I basically copied your code and it does return as a dictionary wich is perfect

Cyber-Kun | 2023-03-06 18:49