I have a JSON file that I parsed into a Godot.Collections.Dictionary.

An example JSON entry looks like this:

"R011": {
        	"ItemName": "TreeBranch",
       	 "ItemCategory": "Resource",
        	"StackSize": 40,
        	"Description": "A sturdy branch used for crafting."
   },

Which is parsed with the method

Godot.Collections.Dictionary LoadData(string filePath)
 {
    	JSONParseResult jsonData;
   		File fileData = new File();

   	 	fileData.Open(filePath, File.ModeFlags.Read);
    	jsonData = JSON.Parse(fileData.GetAsText());
    	fileData.Close();
    	return (Godot.Collections.Dictionary)jsonData.Result;
	}

Which is part of an autoloaded script for the game's databases and returns the entire set of values like this

{Description:A sturdy branch used for crafting., ItemCategory:Resource, ItemName:TreeBranch, StackSize:40}

I wonder if there is a way to get just the value I am looking for. For instance, is there a way to use "Stacksize" as a key to get only R011's Stacksize?

JSON will return a Dictionary, which you can access any element using bracket notation.

var stack_size = save_file["R011"]["StackSize"]

@cybereality said: JSON will return a Dictionary, which you can access any element using bracket notation.

var stack_size = save_file["R011"]["StackSize"]

I tried that but got the error "Cannot apply indexing with [] to an expression of type 'object'"

That is because the JSON text is not formatted correctly. You need to have braces around the information for it to be a Dictionary (or brackets if you want an Array, but in most cases Dictionary is more flexible).

{ 
"R011": {
                "ItemName": "TreeBranch",
             "ItemCategory": "Resource",
                "StackSize": 40,
                "Description": "A sturdy branch used for crafting."
       }
}

@cybereality said: That is because the JSON text is not formatted correctly. You need to have braces around the information for it to be a Dictionary (or brackets if you want an Array, but in most cases Dictionary is more flexible).

{ 
"R011": {
                "ItemName": "TreeBranch",
             "ItemCategory": "Resource",
                "StackSize": 40,
                "Description": "A sturdy branch used for crafting."
       }
}

My apologies for the lack of clarity. I just pasted a single element for example but the JSON has many more and they are contained within a pair of {}

So it looks like

{
"R011": {
     	"ItemName": "TreeBranch",
      	"ItemCategory": "Resource",
      	"StackSize": 40,
       	"Description": "A sturdy branch used for crafting."
    	},


"R012": {
 		"ItemName": "Log",
    	"ItemCategory": "Resource",
    	"StackSize": 0,
   		"Description": "A large log too large for your bag. Used for sculpting"
	 },
	repeat, etc, blah blah,

}

Try putting some print statements and making sure the text is read correctly.

You can also check JSONParseResult.error, there may be some problem with the parsing.

Trying JsonParseResult.error results in JsonParseResult does not contain a definition for error. Json.Parse().Error returns OK.

GD.Print(jsonData[R011]); returns

{Description:A sturdy branch used for crafting., ItemCategory:Resource, ItemName:TreeBranch, StackSize:40}

What get printed if you do this? GD.Print(jsonData[R011][StackSize])

You would want to check:

jsonData.Error

After the parse line. Sorry if I was confusing. But it looks like you are getting something.

I found this, which looks interesting. You might have to cast jsonData[R011] as another Dictionary.

https://godotengine.org/qa/68432/get-sub-keys-from-a-json-file

@cybereality said: You would want to check:

jsonData.Error

After the parse line. Sorry if I was confusing. But it looks like you are getting something.

I found this, which looks interesting. You might have to cast jsonData[R011] as another Dictionary.

https://godotengine.org/qa/68432/get-sub-keys-from-a-json-file

That did it! With a little tweaking, it came to

var stackSize = (jsonData["R011"] as Godot.Collections.Dictionary)["StackSize"];

works like a charm, thank you!

a year later