Search code examples
c#jsondeserializationsystem.text.json.net-8.0

How to deserialize objects having different keys (names) but identical structure in C# .Net 8 using System.Text.Json.Serialization?


Consider following Json data below, I would like to deserialize object items in "cart" as array/Enumerable -lets say CartItem- since ojects "1345516108" and "1456460669" are structurally identical.

{
    "checksum": "8ea325b368df5f2994e3c6da022e52db",
    "totalPrice": {
        "amount": "71.98",
        "symbol": "$",
        "code": "USD",
        "isZero": false,
        "rawAmount": 7198,
        "formattedAmount": "71.98",
        "full": "$ 71.98",
        "for_email": "$ 71.98"
    },
    "cart": {
        "1345516108": {
            "id": 1345516108,
            "price": {
                "currency": "USD",
                "amount": "11.99",
                "baseAmount": "29.99",
                "finalAmount": "11.99",
                "isDiscounted": true,
                "discountPercentage": 60,
                "discountDifference": "18.00",
                "symbol": "$",
                "isFree": false,
                "discount": 60,
                "isBonusStoreCreditIncluded": false,
                "bonusStoreCreditAmount": "0.00",
                "promoId": "weekly_deals_3_november"
            },
            "timeAdded": 1700141748,
            "availability": {
                "isAvailable": true,
                "isAvailableInAccount": true
            },
            "isDiscounted": true,
            "isInDevelopment": false,
            "releaseDate": 1659560400,
            "salesVisibility": {
                "isActive": true,
                "fromObject": {
                    "date": "2022-08-04 19:55:00.000000",
                    "timezone_type": 3,
                    "timezone": "Europe/Nicosia"
                },
                "from": 1659632100,
                "toObject": {
                    "date": "2037-12-31 23:59:59.000000",
                    "timezone_type": 3,
                    "timezone": "Europe/Nicosia"
                },
                "to": 2145909599
            },
            "buyable": true,
            "title": "Hard West 2",            
            "url": "/en/game/hard_west_2",
            "supportUrl": "/support/",
            "forumUrl": null,
            "worksOn": {
                "Windows": true,
                "Mac": false,
                "Linux": false
            },
            "category": "Role-playing",
            "originalCategory": null,
            "rating": 0,
            "type": 1,
            "isComingSoon": false,
            "isPriceVisible": true,
            "isMovie": false,
            "isGame": true,
            "slug": null,
            "isWishlistable": true,
            "extraInfo": [],
            "ageLimit": 0
        },
        "1456460669": {
            "id": 1456460669,
            "price": {
                "currency": "USD",
                "amount": "59.99",
                "baseAmount": "59.99",
                "finalAmount": "59.99",
                "isDiscounted": false,
                "discountPercentage": 0,
                "discountDifference": "0.00",
                "symbol": "$",
                "isFree": false,
                "discount": 0,
                "isBonusStoreCreditIncluded": false,
                "bonusStoreCreditAmount": "0.00",
                "promoId": null
            },
            "timeAdded": 1700141758,
            "availability": {
                "isAvailable": true,
                "isAvailableInAccount": true
            },
            "isDiscounted": false,
            "isInDevelopment": false,
            "releaseDate": 1691010000,
            "salesVisibility": {
                "isActive": true,
                "fromObject": {
                    "date": "2020-10-06 19:50:00.000000",
                    "timezone_type": 3,
                    "timezone": "Europe/Nicosia"
                },
                "from": 1602003000,
                "toObject": {
                    "date": "2037-12-31 23:59:59.000000",
                    "timezone_type": 3,
                    "timezone": "Europe/Nicosia"
                },
                "to": 2145909599
            },
            "buyable": true,
            "title": "Baldur's Gate 3",            
            "url": "/en/game/baldurs_gate_iii",
            "supportUrl": "/support/",
            "forumUrl": null,
            "worksOn": {
                "Windows": true,
                "Mac": true,
                "Linux": false
            },
            "category": "Role-playing",
            "originalCategory": null,
            "rating": 39,
            "type": 1,
            "isComingSoon": false,
            "isPriceVisible": true,
            "isMovie": false,
            "isGame": true,
            "slug": null,
            "isWishlistable": true,
            "extraInfo": [],
            "ageLimit": 0
        }
    },
    "overallAgeLimit": 0
}

I would like to deserialize above json to a class something similar below;

public class Rootobject
{
    public string checksum { get; set; }
    public Totalprice totalPrice { get; set; }
    public Cart cart { get; set; }
    public int overallAgeLimit { get; set; }
}

public class Totalprice
{
    public string amount { get; set; }
    public string symbol { get; set; }
    public string code { get; set; }
    public bool isZero { get; set; }
    public int rawAmount { get; set; }
    public string formattedAmount { get; set; }
    public string full { get; set; }
    public string for_email { get; set; }
}

public class Cart
{
    public cartItem[] cartItems { get; set; }
}

public class cartItem
{
    public int id { get; set; }
    public Price price { get; set; }
    public int timeAdded { get; set; }
    public Availability availability { get; set; }
    public bool isDiscounted { get; set; }
    public bool isInDevelopment { get; set; }
    public int releaseDate { get; set; }
    public Salesvisibility salesVisibility { get; set; }
    
    ...

}

Is it possible to achieve this kind deserialization .Net 8 using System.Text.Json.Serialization?

Thanks.


Solution

  • Your json cart is technically a dictionary id -> CartItem, so change type of your RootObject.cart to IDictionary<string, CartItem> and you should be good.