Trouble parsing JSON file

Godot Version

4.2.2

Question

I am out of practice by like 3 years and I’m trying to bring my old project from 3.6 to 4.2.2 to clarify I was using an edited version of the Godot Open Dialogue by jsena which this is a piece of
In 3.6 my code worked as such

func initiate(file_id, block = 'first'): # Load the whole dialogue into a variable
	id = file_id
	var file = File.new()
	file.open('%s/%s.json' % [dialogues_folder, id], file.READ)
	var json = file.get_as_text()
	dialogue = JSON.parse(json).result
	file.close()
	first(block) # Call the first dialogue block

The variable json gets the file as:

 {
	"first":
	{
		"type": "text",
		"content": "The text isn't important",
		"next": "0"
	},
	"0":
	{
		"type": "text",
		"content": "Second unimportant thing"
	}
}

and when it gets parsed into dialogue it looks like this:

{0:{content:Second unimportant thing, type:text}, first:{content:The text isn't important, next:0, type:text}}

With prereq out of the way
in 4.2.2. I have code that is this

func initiate(file_id, block := 'first'):
	# Load the whole dialogue into a variable
	var id = file_id
	var file = FileAccess.open("%s/%s.json" % [dialogues_folder, id], FileAccess.READ)
	var json := file.get_as_text()
	file.close()
	
	var parse_result  = JSON.parse_string(json)

    dialogue = parse_result
	first(block)

The variable dialogue is outputting

{ "first": { "type": "text", "content": "The text isn't important", "next": "0" }, "0": { "type": "text", "content": "Second unimportant thing" } }

I’m getting the data but it’s in a different order (which seems to be alright? I can’t get the second screen textbox to work but I think it’s because it’s formatted different from before. If there is only one screen of dialogue it works) in 3.6 parsing it got rid of all the extra quotation marks

If I add these lines after the var dialogue = JSON.parse_string(json) and replace the final dialogue to take cleanJson

var cleanJson = JSON.stringify(parse_result)
cleanJson = cleanJson.replace('\"','')
cleanJson = cleanJson.replace(',',', ')

dialogue = cleanJson

dialogue

{0:{content:Second unimportant thing, type:text}, first:{content:The text isn't important, next:0, type:text}}

That “cleans up” the data but then anytime I have a quotation mark or comma in my dialogue it would get messed up. It’d work mostly but the edge cases would break it. Additionally later in the code dialogue uses a has method so I need to keep it as a dictionary…
This all feels hacky and like I’m doing it wrong. Any help would be greatly appreciated.

Your edited version of the godot-open-dialogue system only accepts this dialogue format without quotes? It seems like it would be easier to just work with the json. If you absolutely need this format, you could loop over the items in the json output and create a new string:

var json_output = JSON.parse_string(json)
var dialogue_string = ""
for item in json_output.keys():
      var item_json = json_output[item]
      dialogue_string += "{" + item + ":{"
      for inneritem in item_json.keys():
            var inneritem_json = item_json[inneritem]
            dialogue_string += inneritem + ":"
            dialogue_string += item_json[inneritem]+ ","
      dialogue_string += ","

But I’d recommend you find a way to use the json instead.

1 Like

Hey Snipercup thank you so much for responding! I was actually thinking of closing or deleting the post but I wasn’t sure what to do with it.

It looks like I made an incorrect assumption when writing this, I had assumed that it was having trouble finding the next set of dialogue since originally it didn’t have the quotations marks. I switched where I was focusing on and returned to this today and it looks like it does work. I wrote a new json test file that has three pages of dialogue that worked. It’s the special characters that were being used for line breaks (@) or a longer typing effect (|) caused the program to hang and then crash. So I was looking in the wrong section… So now I’ve got to problem solve that section, we are making progress though!

Thank you for the proposed help though!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.