Newtonsoft .NET Deserialization

BBilnord1/30/2023
Hey, I would like to deserialize JSON string but I'm actually out of ideas. I have tried JToken to do it by path but it was failure for me. It's complicated for me because in the items_list objects starts with item name and I just can not make class to deserialize the json as far as I know. I suppose that there is easy way to do it but I do not have that much knowledge in JSON. Here's my JSON example. Appreciate any ideas/help.
{
    "success": true,
    "currency": "USD",
    "timestamp": 1674493571,
    "items_list": {
        "&#39Blueberries&#39 Buckshot | NSWC SEAL": {
            "name": "&#39Blueberries&#39 Buckshot | NSWC SEAL",
            "marketable": 1,
            "tradable": 1,
            "classid": "4114517977",
            "icon_url": "----",
            "icon_url_large": null,
            "type": null,
            "rarity": "Exceptional",
            "rarity_color": "8847ff",
            "price": {
                "7_days": {
                    "average": 3.19,
                    "median": 3.18,
                    "sold": "289",
                    "standard_deviation": "12.46",
                    "lowest_price": 0.99,
                    "highest_price": 3.61
                },
                "30_days": {
                    "average": 2.84,
                    "median": 2.8,
                    "sold": "2835",
                    "standard_deviation": "11.32",
                    "lowest_price": 0.99,
                    "highest_price": 3.61
                },
                "all_time": {
                    "average": 1.41,
                    "median": 2.38,
                    "sold": "157819",
                    "standard_deviation": "51.15",
                    "lowest_price": 0.83,
                    "highest_price": 3.61
                }
            },
            "first_sale_date": "1607036400"
        }
    }
}
BBilnord1/30/2023
I have also tried some JSON to C# Generators but failed.
NneSHa1/30/2023
have you tried like JsonConvert.DeserializeObject(jsonfile) ?
NneSHa1/30/2023
and store it into dynamic variable so you can test to see if it deserialize, if it work after that create strongly typed model to deserialize and you done
FFyren1/30/2023
What's your project? .net core, .net framework, or just .net?
The reason I ask is because System.Text.Json exists and is what should be used instead of Newtonsoft
FFyren1/30/2023
first order of business is making sure your models match the json.
There exists some plugin for Visual Studio that can autogenerate C# class based on json, or you can use some external website for it (json2csharp).
FFyren1/30/2023
As soon as you've generated those files, you should be able to do what @neSHa said if you'd rather keep using newtonsoft, or you can use System.Text.Json.JsonSerializer.Deserialize<MyClass>(json); to achieve the same result (where MyClass is the root level class of your json structure).
FFyren1/30/2023
reason I recommend newtonsoft is because it is not an external package
NneSHa1/30/2023
isnt JsonSerializer part of .net framework, and Newtonsoft.Json third-party library? or am i worng
NneSHa1/30/2023
Newtonsoft.Json have a lot more features and options compared to JsonSerializer. but if he doesnt need anything specific, its better to use JsonSerializer for simplify
FFyren1/30/2023
newtonsoft is third party and introduces a need for frequent security patching and is in that way friction you don't need.
There's very little in newtonsoft that isn't also in STJ
BBilnord1/30/2023
.NET Core
NneSHa1/30/2023
well didnt know that, i through there is lot of difference between these two, where is Newtonsoft.Json bigger, will check them out in details for sure now
FFyren1/30/2023
I am speaking for the commonplace usage 🙂 There may be more features for advanced use, but for simple serializing/deserializing with some jsonserializer options then stj is fine
BBilnord1/30/2023
The auto generators are making class for every element in items_list, I have in the list more than 2000 elements and I can not imagine 2000 classes in my code like this
public class 39Blueberries39BuckshotNSWCSEAL
    {
        public string name { get; set; }
        public int marketable { get; set; }
        public int tradable { get; set; }
        public string classid { get; set; }
        public string icon_url { get; set; }
        public object icon_url_large { get; set; }
        public object type { get; set; }
        public string rarity { get; set; }
        public string rarity_color { get; set; }
        public Price price { get; set; }
        public string first_sale_date { get; set; }
    }

    public class 39MediumRare39CrasswaterGuerrillaWarfare
    {
        public string name { get; set; }
        public int marketable { get; set; }
        public int tradable { get; set; }
        public string classid { get; set; }
        public string icon_url { get; set; }
        public object icon_url_large { get; set; }
        public object type { get; set; }
        public string rarity { get; set; }
        public string rarity_color { get; set; }
        public Price price { get; set; }
        public string first_sale_date { get; set; }
    }

    public class 39TheDoctor39RomanovSabre
    {
        public string name { get; set; }
        public int marketable { get; set; }
        public int tradable { get; set; }
        public string classid { get; set; }
        public string icon_url { get; set; }
        public object icon_url_large { get; set; }
        public object type { get; set; }
        public string rarity { get; set; }
        public string rarity_color { get; set; }
        public Price price { get; set; }
        public string first_sale_date { get; set; }
    }
FFyren1/30/2023
That is very weird. What did you use? json2csharp.com or something else?
BBilnord1/30/2023
json2csharp.com
NneSHa1/30/2023
why cant you use dynamic to check whenever is it deserializing in good way first?
BBilnord1/30/2023
what you mean by use dynamic, I did
var json = JsonConvert.DeserializeObject(myJsonString);
            Console.WriteLine(json.ToString());
and its working if you meant this
FFyren1/30/2023
So most class generators try to trim elements down to a common shared class, and you will get multiple instances of these. If your dataset is sane, then you should have nowhere near 2000 classes. But maybe a two-digit amount of classes, sure.
FFyren1/30/2023
I was working with the spotify api once, and when deserializing the json for a playlist I ended up with 25 classes I think. That's not uncommon.
FFyren1/30/2023
There could also be a problem with your dataset
FFyren1/30/2023
for example:

{
  "ABookAboutWar":{
    "someProperty":"someValue"
  },
  "ABookAboutLove":{
    "someProperty":"someValue"
  }
}


This will be deserialized into a class that has two properties, one named "ABookAboutWar" for example. In a more sane dataset you would instead have this stored as elements in an array.
BBilnord1/30/2023
"items_list": {
          {
            "name": "&#39Blueberries&#39 Buckshot | NSWC SEAL",
            "marketable": 1,
            "tradable": 1,
            "classid": "4114517977",
            "icon_url": "----",
            "icon_url_large": null,
            "type": null,
            "rarity": "Exceptional",
            "rarity_color": "8847ff",
            "price": {
                "7_days": {
                    "average": 3.19,
                    "median": 3.18,
                    "sold": "289",
                    "standard_deviation": "12.46",
                    "lowest_price": 0.99,
                    "highest_price": 3.61
                },
                "30_days": {
                    "average": 2.84,
                    "median": 2.8,
                    "sold": "2835",
                    "standard_deviation": "11.32",
                    "lowest_price": 0.99,
                    "highest_price": 3.61
                },
                "all_time": {
                    "average": 1.41,
                    "median": 2.38,
                    "sold": "157819",
                    "standard_deviation": "51.15",
                    "lowest_price": 0.83,
                    "highest_price": 3.61
                }
            },
            "first_sale_date": "1607036400"
          }
    }
That's how the data should look to avoid creating as many classes as the items_list contains elements right?
FFyren1/30/2023
No, not quite. You're not using an array here
FFyren1/30/2023
in JSON an array is []
FFyren1/30/2023
{
  "items_list": []
}
FFyren1/30/2023
This would make the contents inside items_list deserialize into an array
FFyren1/30/2023
instead of one class per element
FFyren1/30/2023
Do you understand the difference between {} and [] in json?
BBilnord1/30/2023
Yes
FFyren1/30/2023
Then.. in your json, the one you posted above - your "items_list" isn't actually a list.
It's a class with an anonymous object which has many properties like name, marketplace, tradeable etc
BBilnord1/30/2023
I know but tried to look for a way to deserialize it without making a lot of classes, thats not my created json I'm just working with it but gonna drop it and make a own one
FFyren1/30/2023
{
  "MyElements": [
    {
      "Name":"",
      "Marketable":"",
      "Tradeable":""
    },
    {
      "Name":"",
      "Marketable":"",
      "Tradeable":""
    }
  ]
}
FFyren1/30/2023
Something like this would work as a list
FFyren1/30/2023
MyElements would get serialized into List<Element> where Element is just something that contains the three properties seen here
BBilnord1/30/2023
Thanks for yours help Fyren and neSHa gonna close the thread.