Hi I have a following JObject
{
"$id": "1",
"CustomerApplication": {
"contractDate": {
"$id": "2",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": false,
"Date": "2020-07-05T00:00:00"
},
"lastTouchedDate": {
"$id": "4",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": true,
"Date": null
},
"totalIncome": 110000,
"createdByID": 0,
"createdDate": {
"$id": "5",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": false,
"Date": "2020-08-04T12:26:28.22"
},
"modifiedByID": 37683,
"modifiedDate": {
"$id": "6",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": false,
"Date": "2020-08-04T12:35:52.433"
}
},
"Customers": {
"$values": [
{
"custLicense": "555555",
"custLicenseState": "TX",
"dateOfBirth": {
"$id": "11",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common.V4, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": false,
"Date": "1980-08-03T00:00:00"
},
"includeDebt": true,
"customerNumber": "1234567",
"phone": "1234578",
"pager": "",
"mobilePhone": " ",
"memo": "",
"residences": {
"$id": "12",
"$type": "Customer.Residence[], Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"$values": [
{
"$id": "13",
"$type": "Customer.Residence, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"residenceID": 265906636,
"applicantID": 142874336,
"applicationID": 110315632,
"residentialStatusID": 0,
"modifiedByID": 0,
"modifiedDate": {
"$id": "14",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": false,
"Date": "2020-08-04T12:26:36.953"
}
}
]
}
}
]
}
}
In my above json body, I have nested objects as shown in example below:
"contractDate": {
"$id": "2",
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
"IsDBNull": false,
"Date": "2020-07-05T00:00:00"
},
So what I would like is to identify all properties which have type as follows
"$type": "Customer.DateTimeObjectDbNullConverter, Customer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=f291d57f62344",
and flatten them to the "Date" property value of that object like this:
"contractDate" :"2020-07-05T00:00:00"
I want to do this for all such properties at various nested levels of my Json object. How can I achieve them..
this is what I have tried so far and works for most of my Jsons.
// When The Type is of JArray
JObject result= JObject.Parse(strStream);
var jsonObjItems = result["$values"];
JArray objArray = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(jsonObjItems.ToString());
foreach (var item in objArray.Children())
{
JObject joItem = (JObject)item;
var lstNestedObjects = joItem.Children().Values().ToList().Where(x => x.Type == JTokenType.Object).ToList();
foreach (var newObject in lstNestedObjects)
{
var propValueType = newObject.Children().Values().Where(x =>
x.Value<string>().Contains("DateTimeObjectDbNullConverter"))
.ToList();
if (propValueType.Count == 1)
{
var propName = newObject.Path.ToString().Split(".")[1];
var propValue = newObject.Children().Values().Where(x => x.Type == JTokenType.Date).ToList().FirstOrDefault();
joItem.Property(propName).Remove();
joItem.Add(propName, propValue);
}
}
}
result["$values"] = objArray;
return result;
But what I need is more like a generic method that can traverse through n number of Nested levels and change it . Any help is appreciated.