0 votes

Inside a JSON file is:
This is Text 1
This is Text 2

How would I be able to take that text and turn it into two strings which I can then assign as separate array elements?

I know I need a export(String, FILE, "*.json") var file_path which is a reference to the JSON file, where do I go from there?

in Engine by (48 points)

A JSON file should have content like this:

{ 
    "text1" : "This is Text1",
    "text2" : "This is Text2"
}

The content you wrote in the question doesnt represent a valid json content.

Is there any reason why it should be like that? Is there an issue with the json content I provided in my answer?

The answer you provided is fine, but is just using an array as a json array instead of a json object. You can refer to json.org for specification. The one that was wrong was what you wrote in your initial question, which was just plain text.

In summary, this is a JSON array:

[ "Ford", "BMW", "Fiat" ] 

This is a JSON object, that can contain arrays:

{
  "name":"John",
  "age":30,
  "cars":[ "Ford", "BMW", "Fiat" ]
}

A JSON file can contain multiple arrays, or objects.

Note that for completeness' sake, scalar data types such as "hello" are considered valid JSON when parsed as a standalone document. (This may not apply to Godot's implementation, though.)

1 Answer

0 votes
Best answer
export(String, FILE, "*.json") var file_path    
var array : Array

func parse_test():
var file = File.new()
file.open(file_path, file.READ)
var text = file.get_as_text()
print(text)
var p = JSON.parse(text)

if typeof(p.result) == TYPE_ARRAY:
    var i = 0
    while i < p.result.size():
        dialogue.push_back(p.result[i])
        i+=1
else:
    print("unexpected results")

This, with a file path to a json file containing ["hello", "world", "!"] was the solution I found

by (48 points)
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 Frequently asked questions and 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 [email protected] with your username.