C
C#

help

source and target

Mmalkav9/29/2022
Say I have two Dictionary<string,JValue> items. and I want to return a new JsonConvert.SerializeObject(new Dictionary<string,JValue>()) of all the items in the first item. How would I go about it? Do I go for a foreach loop? or can i do it easier with a Linq expression or something?? Example (Source and target in the first example):
{
"records": [
{
"BookID": "1",
"BookTitle": "SQL World",
"BookPrice": "550"
},
{
"BookID": "2",
"BookTitle": "Oracle Overview",
"BookPrice": "780"
}
]
}

{
"records": []
}
{
"records": [
{
"BookID": "1",
"BookTitle": "SQL World",
"BookPrice": "550"
},
{
"BookID": "2",
"BookTitle": "Oracle Overview",
"BookPrice": "780"
}
]
}

{
"records": []
}
// we grab the array of the first JSON here
Dictionary<string,JValue> itemOne = JsonConvert.DeserializeObject<Dictionary<string,JValue>>(data1.records)
// we grab the array of the second JSON here
Dictionary<string,JValue> itemTwo = JsonConvert.DeserializeObject<Dictionary<string,JValue>(data2.records);

// in this case the keys of ItemOne do not exist in itemTwo, so return a Json object that looks like this:
Dictionary<string,string[]> returnItem = new() { "results", new[] { /* each of the itemOne items (key, value pair) as string*/ }
// we grab the array of the first JSON here
Dictionary<string,JValue> itemOne = JsonConvert.DeserializeObject<Dictionary<string,JValue>>(data1.records)
// we grab the array of the second JSON here
Dictionary<string,JValue> itemTwo = JsonConvert.DeserializeObject<Dictionary<string,JValue>(data2.records);

// in this case the keys of ItemOne do not exist in itemTwo, so return a Json object that looks like this:
Dictionary<string,string[]> returnItem = new() { "results", new[] { /* each of the itemOne items (key, value pair) as string*/ }
Ok, this is what I think it should become
Dictionary<string, JValue> sourceRecords = Pack(JsonConvert.DeserializeObject<string[]>(data.SourceJson));
Dictionary<string, JValue> targetRecords = Pack(JsonConvert.DeserializeObject<string[]>(data.TargetJson));

Dictionary<string, JValue> insertRecords = sourceRecords.Keys.Where(item => !targetRecords.ContainsKey(item))
.ToDictionary(item => item, item => sourceRecords[item]);

Dictionary<string, JValue> updateRecords = sourceRecords.Keys
.Where(item => targetRecords.ContainsKey(item) && targetRecords[item].Equals(sourceRecords[item]))
.ToDictionary(item => item, item => targetRecords[item]);

// now turn everything into an array and Json object
Dictionary<string, Dictionary<string, JValue>> result = new(){{ "Insert", insertRecords}, {"Update", updateRecords}};

return new OkObjectResult(JsonConvert.SerializeObject(result));
Dictionary<string, JValue> sourceRecords = Pack(JsonConvert.DeserializeObject<string[]>(data.SourceJson));
Dictionary<string, JValue> targetRecords = Pack(JsonConvert.DeserializeObject<string[]>(data.TargetJson));

Dictionary<string, JValue> insertRecords = sourceRecords.Keys.Where(item => !targetRecords.ContainsKey(item))
.ToDictionary(item => item, item => sourceRecords[item]);

Dictionary<string, JValue> updateRecords = sourceRecords.Keys
.Where(item => targetRecords.ContainsKey(item) && targetRecords[item].Equals(sourceRecords[item]))
.ToDictionary(item => item, item => targetRecords[item]);

// now turn everything into an array and Json object
Dictionary<string, Dictionary<string, JValue>> result = new(){{ "Insert", insertRecords}, {"Update", updateRecords}};

return new OkObjectResult(JsonConvert.SerializeObject(result));
Tell me if I'd be doing it wrong?

Looking for more? Join the community!