1

I need someone wiser than myself to guide me to creating some nested dictionaries in C#. I'm trying to create a post in restsharp that would ultimately resemble this sample json:

{
    "brandId": 34344,
    "collectionId": 5,
    "productTypeId": 1,
    "identity": {
        "sku": "SKU0001",
        "ean": "12323423",
        "upc": "543534563",
        "isbn": "54353453",
        "barcode": "45453"
    },
    "stock": {
        "stockTracked": true,
        "weight": {
            "magnitude": 4324.54
        }
    },
    "financialDetails": {
        "taxable": false,
        "taxCode": {
            "id": 7,
            "code": "T20"
        }
    },
    "salesChannels": [
        {
            "salesChannelName": "Brightpearl",
            "productName": "Product B",
            "productCondition": "new",
            "categories": [
                {
                    "categoryCode": "276"
                },
                {
                    "categoryCode": "295"
                }
            ],
            "description": {
                "languageCode": "en",
                "text": "Some description",
                "format": "HTML_FRAGMENT"
            },
            "shortDescription": {
                "languageCode": "en",
                "text": "Some description",
                "format": "HTML_FRAGMENT"
            }
        }
    ],
    "seasonIds": [
        1,
        2,
        3
    ],
    "nominalCodeStock": "1000",
    "nominalCodePurchases": "5000",
    "nominalCodeSales": "4000",
    "reporting": {
        "seasonId": 3,
        "categoryId": 295,
        "subcategoryId": 298
    }
}

I have not tried to created nested dictionaries before. My experiences has been more limited to this:

Dictionary<string, string> values = new Dictionary<string, string>
{
    { "brandid", "1234" },
    { "productTypeId", "11" }
};

string json = JsonConvert.SerializeObject(values);


List<Dictionary<string, string>> ld = new List<Dictionary<string, string>>
{
    values
};

request2.AddJsonBody(ld);

Some help pointing me in the right direction would be immensely appreciated.

2 Answers2

1

I'd suggest that you create the object. Something like this:

public class Portfolio {
    public int brandId;
    public int collectionId;
    public int productTypeId;
    public Identity identity
    // etc
}

public class Identity {
   public string sku;
   // etc
}

And then create a new portfolio object, serialize it and send it over the wire.

var portfolio = new Portfolio
{
    // initialize values here
};

string json = JsonConvert.SerializeObject(portfolio);
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
0

That doesn't work well with dictionaries because there are multiple types for the dictionary value (number, string, sub dictionary, ...).

I'd recommend to create real types instead of dictionaries, that's less work in the end:

// types
public record MyValue(int BrandId, int CollectionId, Identity Identity, [...]){}
public record Identity(string Sku, string, Ean, string Isbn, string Barcode){}
...

// use it
var data = new MyValue(123, 456, new("mysku", "myean", "myisbn", "mybarcode"));
var response = await httpClient.postAsJsonAsync(data);

You'll find the docs here:

You'll also have to tell the json serializer to convert from C# property naming style (AbcDef) to camel case (abcDef), see: ASP.NET Core 3.0 System.Text.Json Camel Case Serialization

Note that this is c# 9 (.net 5.0)

If you want to do it "freestyle", take a look on the dynamic type https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/reference-types#the-dynamic-type (opinion: don't)

Christoph Lütjen
  • 5,403
  • 2
  • 24
  • 33