3

I am reading a json-string from an API (in a script component i SSIS). The json looks something like the one below (this is only one record, the real string contains many more). When trying to deserialize into a class containing the needed properties, I would like to put the "value" for the "costCenters"-property into a string property in my table. How ever, since the costCenters-value in itself contains a JSON, the deserialization fails (because it encounters the objects inside it).

If I exclude the property CostCenters from my class, it manages to deserialize the other properties just fine. I would think (hope) that it would be possible to force the inner JSON into a string property? Any suggestions?

This is my class that is used for the deserilazing:

internal class Unit
{
    public int ParentId { get; set; }
    public int CompanyId { get; set; }
    public string Abbreviation { get; set; }
    public string AbbreviationPath { get; set; }
    public int Manager { get; set; }
    public int UnitId { get; set; }
    public string Name { get; set; }
    public string LevelDescription { get; set; }
    public int OrfuCompanyId { get; set; }
    public string Resh { get; set; }

    //If the below propery is commented out, the deserializing works fine.
    public string CostCenters { get; set; }
    public int Level { get; set; }
    public int OrfuUnitId { get; set; }
}

This is how I call the deserializer for NewtonSoft:

var units = new List<Unit>();
units = JsonConvert.DeserializeObject<List<Unit>>(jsonString);

This is how the jsonString looks (edited):

[
  {
    "$id": "1",
    "parentId": 999,
    "companyId": 9123,
    "abbreviation": "ZZZ",
    "abbreviationPath": "SOMEPATH",
    "costCenters": [
      {
        "$id": "12",
        "costCenter": "12345",
        "costCenterSourceId": "99",
        "costCenterSource": "TBF",
        "costCenterTypeId": "999",
        "costCenterType": "TypeOfCostCenter",
        "startDate": "2018-01-01T00:00:00",
        "endDate": "9999-12-31T00:00:00"
      },
      {
        "$id": "88",
        "costCenter": "191945444",
        "costCenterSourceId": "88",
        "costCenterSource": "TBB",
        "costCenterTypeId": "15",
        "costCenterType": "SomeTextHere",
        "startDate": null,
        "endDate": null
      }
    ],
    "manager": 12345678,
    "deputy": 0,
    "homeShare": "\\\\someaddress.net\\someFolder\\SomeCode",
    "objectGuid": "ThisIsAGUID",
    "distinguishedName": "OU=ABC,OU=NNN,OU=FFF,OU=HHH,OU=HNV,OU=IDK,DC=heipaadeg,DC=com",
    "orfuUnitId": 9125,
    "orfuCompanyId": 9123,
    "resh": "123456789",
    "nis": "",
    "unitId": 4321,
    "name": "2 some name",
    "level": 9,
    "levelDescription": "Level number 4"
  }
]
dbc
  • 104,963
  • 20
  • 228
  • 340
GHauan
  • 189
  • 1
  • 13
  • Why do you need CostCenters as a string , not as an array? – Serge Jan 11 '22 at 17:49
  • 1
    I'm not sure I understand your problem. Are you saying you want to deserialize `CostCenters` from a JSON array into a raw JSON string and then re-serialize it as an embedded JSON string literal? If so you could do that with a custom `JsonConverter`. Does [Efficiently get full json string in JsonConverter.ReadJson()](https://stackoverflow.com/q/56944160/3744182) and your question sufficiently? – dbc Jan 11 '22 at 18:09
  • 1
    This may be exactly what you want: [C# How to save part of nested JSON into object property, but not deserialized?](https://stackoverflow.com/a/65819006/3744182). – dbc Jan 11 '22 at 18:28
  • @Serge Sorry for late reply, I had to log off yesterday. The reason I need it as a string, is that I am about to replace third party component i SSIS that dumps the whole record into a SQL-table, and the whole CostCenters-string is dumped into a varchar(max) column. Thank you all for your suggestions, I will review them and see if some of them will work for me. – GHauan Jan 12 '22 at 10:23

1 Answers1

2

One workaround is to add a field to Unit called CostCentersString in which the CostCenters list is re-serialized:

In Unit class definition:

...

public List<dynamic> CostCenters { get; set; }

public String CostCentersString { get; set; }

Then use:

List<Unit> units = JsonConvert.DeserializeObject<List<Unit>>(jsonString);

foreach (Unit u in units)
{
    u.CostCentersString = JsonConvert.SerializeObject(u.CostCenters);
}
Hussain Khalil
  • 1,520
  • 2
  • 15
  • 24
  • 1
    Thank you. Belive it or not, but this possible way of doing it came to me in bed last night. But I wouldn't thought of using the dynamic-list. This worked as a charm and I was able to do what I needed. – GHauan Jan 12 '22 at 12:14