JSON string help

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

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?

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.

p7f | 2020-07-28 00:14

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

Aravash | 2020-07-28 14:07

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.

p7f | 2020-07-28 14:12

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.)

Calinou | 2020-07-28 14:46

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