How to open a JSON file in Godot that contains 2 arrays?

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
  • #1
Darkmisc
204
27
TL;DR Summary
I've saved two arrays in a JSON file, but can't seem to open them again.
Hi everyone

I've saved two arrays into a JSON file.

This was the code I used:
save:
func save():
    var file = FileAccess.open("user://savedarrays.json", FileAccess.WRITE)
    var file_path = "user://saved_arrays.json"
    
    var data = {
        "arrayA": Global.arrayA,
        "arrayB": Global.arrayB
    }
    var json_string: String = JSON.stringify(data)
    file.store_string(json_string)
    file.close()
    print("Arrays saved successfully.")

When I open the file in notepad, this is what I get:
{"arrayA":["France","Spain","UK","Russia"],"arrayB":["Paris","Madrid","London","Moscow"]}

I'm using the following code to load the arrays:

load:
func loadArraysFromFile():
    
    var file = "user://savedarrays.json"
    var file_text = FileAccess.get_file_as_string(file)
    var parse_result = JSON.parse_string(file_text)
    print(parse_result)
            
    if parse_result.error == OK:
        var parsed_data: Dictionary = parse_result.result as Dictionary
        
        if parsed_data.has("arrayA"):
            Global.arrayC = parsed_data["arrayA"]
            print(Global.arrayC)
        if parsed_data.has("arrayB"):
            Global.arrayD = parsed_data["arrayA"]
            print(Global.arrayD)

It prints out <null> as the parse_result.

Does anyone know what I've done wrong with the load code?

I used JSON because I read somewhere that that's what I need if I want to save two arrays under the one filename.

Elsewhere, I've used the following code to save one variable to a file:
save one var:
    var file = FileAccess.open(save_path, FileAccess.WRITE)
    file.store_var(Global.thing)
Thanks
 
Technology news on Phys.org
  • #2
Nvm. I saved the two arrays into a dictonary and saved the dictionary without using JSON.
 

1. How do I load a JSON file in Godot that contains two arrays?

To load a JSON file in Godot, first ensure your JSON file is in the Godot project's folder structure. You can use the `File` class to open and read the file, and then utilize the `parse_json()` function to convert the JSON string data into Godot's data types (like arrays or dictionaries). Here’s a basic example:

var file = File.new()if file.open("res://your_file.json", File.READ) == OK:    var json_text = file.get_as_text()    var data = parse_json(json_text)    file.close()else:    print("Failed to open file.")

2. How do I access the arrays inside the JSON data in Godot?

Once the JSON file is loaded and parsed, accessing the arrays depends on the structure of your JSON. If your JSON file has two arrays at the root level, you can access them directly by their index or key if they are part of an object. For example, if the JSON structure is `{"array1": [...], "array2": [...]}`, you can access them like this:

var array1 = data["array1"]var array2 = data["array2"]

3. What are common errors to watch out for when loading JSON files in Godot?

Common errors include file path issues, JSON syntax errors, and runtime errors if the JSON structure is accessed incorrectly. Ensure your file path is correct and relative to the `res://` or `user://` directories. Use JSON validators to check your JSON file's syntax. Lastly, make sure you access data in your JSON correctly according to its hierarchy and type (e.g., using correct keys for dictionaries).

4. Can I modify the JSON data and save it back to the file in Godot?

Yes, you can modify the JSON data once it's loaded into Godot. After making changes, you can convert the data back to a JSON string using `to_json(data)` and write it back to the file using the `File` class. Here is how you might write modified data back to a JSON file:

var file = File.new()if file.open("res://your_file.json", File.WRITE) == OK:    file.store_string(to_json(data))    file.close()else:    print("Failed to open file for writing.")

5. How can I handle large JSON files efficiently in Godot?

For large JSON files, consider reading and processing the file in chunks to avoid high memory usage, or preprocess the file to include only the necessary data before loading it into Godot. Additionally, you might want to look into asynchronous loading (`yield` with `Thread` or background processing) to prevent freezing the UI when dealing with large files.

Similar threads

  • Programming and Computer Science
Replies
4
Views
387
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Mechanical Engineering
Replies
23
Views
36K
Back
Top