How can I convert this json file to be nested dictionary/List

Hi, I’m a beginner, I’ve spent a day trying to use CSV or JSON kind of file to give input to change some values in my game. My thought is if I can convert below JSON file to a nested dictionary/List and then I can use a dictionary/List for updating the values. But this JSON file and its data struct are too complex for me.

Attached below is the JSON file.

“Player” represents a GameObject that has speedIncrease variable, Regular A, B, C D represents GameObject has members, Amount, Probability, CanAtStart, and AtEndOnly. Eventually, I want when the level is 1, I use 1’s data to update the Player and RegularX GameObject member’s value, and when it’s 2 use 2’s data to update them etc.

The question is how the nested dictionary/List should look like and how can I convert this JSON file to be?

"1": {
"Player": {
"SpeedIncrease": 1
},
"RegularA": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
},
"RegularB": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
},
"RegularC": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
},
"RegularD": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
}
},
"2": {
"Player": {
"SpeedIncrease": 1
},
"RegularA": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
},
"RegularB": {
"Amount": 1,
"Probability": 2,
"CanAtStart": "",
"AtEndOnly": ""
},
"RegularC": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
},
"RegularD": {
"Amount": 1,
"Probability": 1,
"CanAtStart": "",
"AtEndOnly": ""
}
},
3: {
..........```

Use this https://json2csharp.com/

Then deserialize the json

ALSO: note that Unity’s JSON will not handle a Dictionary.

Yes, you read that correctly.

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

Also, always be sure to leverage sites like:

https://json2csharp.com <— as All_American pointed out above
https://csharp2json.io

1 Like

These answers are not the best, I do this all the time in Unity without loading external packages. Newtonsoft Json has been included in Unity for years. Add it from the package manager (Although it is likely already installed).

Sample Code
var document = JsonConvert.DeserializeObject(jsonContent);

Uhm…

Kurt recommended this already 2 years ago. So there was no need to revive this thread.

Yes, some other packages may depend on it, but assuming it’s already there is ill adviced. It completely depends on what other packages you may use. When you have a fresh Unity project, it would not be included.

1 Like