Loading NPC Data from JSON File (Dictionary)

Godot Version

4.3

Question

`Hey… So I am making a Game in Godot, I am at the moment trying to create NPC Enemies from an (enemies.json) JSON File.

Its structure is:

{
     "Username1": "SampleUsername"{
          "Attack": "5",
          "Defense": "2"
     },
     "Username2": "SampleUsername2"{
          "Attack": "7",
          "Defense": "4"
     }
}

So it is a Dictionary but the JSON file crashes the game on:

if json_data.error == OK:
     NextPart

This is my related script if you can help: Loading NPC Data from JSON File (Dictionary) - Pastebin.com

`

That’s not a valid JSON file. Try this instead:

{
     "Username1": {
          "Attack": "5",
          "Defense": "2"
     },
     "Username2": {
          "Attack": "7",
          "Defense": "4"
     }
}

Yeah, that was a tpyo… I wasn’t using my code, I was just explaining what the JSON file looked like…

This is my JSON file:

{
  "Anon22310001": {
	"EnemyCountry": "United States",
	"EnemyIP": "192.168.132.140",
	"EnemyMoney": 495,
	"EnemyLevel": 1,
	"EnemyXP": 0,
	"EnemyNexp": 100,
	"EnemyCloudSpace": 200,
	"EnemyAttack": 9,
	"EnemyPenetration": 4,
	"EnemyDefense": 9,
	"EnemySurvellience": 1,
	"EnemyHealth": 225,
	"EnemyDamage": 8
  },
  "Anon22002193": {
	"EnemyCountry": "United Kingdom",
	"EnemyIP": "192.168.220.342",
	"EnemyMoney": 750,
	"EnemyLevel": 2,
	"EnemyXP": 0,
	"EnemyNexp": 100,
	"EnemyCloudSpace": 200,
	"EnemyAttack": 11,
	"EnemyPenetration": 5,
	"EnemyDefense": 11,
	"EnemySurvellience": 2,
	"EnemyHealth": 270,
	"EnemyDamage": 11
  }
}

However I don’t think my error is with the JSON File.
The error is thrown in my NPCs.gd (load_enemy_data func):


func load_enemy_data(file_path: String) -> void:
	var file = FileAccess.open(file_path, FileAccess.READ)
	print("FILE OPENED...")
	if file:
		var json_data = JSON.parse_string(file.get_as_text())
		print("WHETHER SUCCESSFUL OR NOT JSON PARSED...")
		if json_data.error == OK:
			print("IS JSON_DATA MORE THAN 0?")
			if typeof(json_data.result) == TYPE_DICTIONARY:
				enemy_data_dict = json_data.result
				print("Loaded Enemy Data Successfully")
				file.close()
			else:
				print("JSON Error: Parsed Data is not a Dictionary")
				
		else:
			print("Error parsing JSON: ", json_data.error_string)
		file.close()
	else:
		print("Failed to open File: ", file_path)
	

All of my print("Wierd data within print) is just to debug so I know where the problem is…

I read the docs, and unless I understood it wrong, JSON.parse_string() doesn’t allow you to use (json_data.error)…

Specifically .error cannot be used in a parse_string() func…

I need to use JSON.parse() func in order to use .error == OK but I am still struggling to do that… Lol

The problem here is because you doing an error check that doesn’t exist, JSON.parse_string will return the value directly or null if fails, not an error enum.

Also JSON.parse will return the error enum directly, so json_data.error is also wrong for both methods.