C
C#

help

Creating an Array of strings from a stringified JSON

Mmalkav9/29/2022
So I have this kind of JSON structure:
{
"sourceJson": [
{
"BookID": "1",
"BookTitle": "SQL World",
"BookPrice": "550"
},
// ...
],
"targetJson": [
{
"BookID": "6",
"BookTitle": "High Availability",
"BookPrice": "900"
}
]
}
{
"sourceJson": [
{
"BookID": "1",
"BookTitle": "SQL World",
"BookPrice": "550"
},
// ...
],
"targetJson": [
{
"BookID": "6",
"BookTitle": "High Availability",
"BookPrice": "900"
}
]
}
and I want to use Newtonsoft.Json to DeserializeObject<string[]>(sourceJson) but when I do this, I get "Unexpected character '{' at path: sourceJson line 3 position 5" Does anyone know how to convert this json into a string[] or IEnumerable<string>? Perhaps I have to use JArray instead?? I don't know
DDaVinki9/29/2022
I think you should consider creating objects out of the json source and target arrays instead of just getting string arrays out of them
CCisien9/29/2022
What are you expecting in the resulting array?
Mmalkav9/29/2022
So sourceJson should become (eventually) a Dictionary<string, JToken> (or equivalent of JToken where the type is undetermined beforehand) and so must targetJson and then I want to do a bunch of comparisons to keys and values before merging or joining the two and returning a result based on that. I guess eventually this is what I attempt to do: Dictionary<string, JValue> insertRecords = sourceRecords.Keys.Where(item => !targetRecords.ContainsKey(item)).ToDictionary(item => item, item => sourceRecords[item]);
CCisien9/29/2022
Create an object with two properties, sourceJson, TargetJson that are arrays of that dictionary Serialize to that
Mmalkav9/29/2022
basically the result should become like this:
{
"update": [
{
"bookId": "6",
"bookTitle": "High Availability",
"bookPrice": "900"
}
],
"inject": [
{
"bookId": "1",
"bookTitle": "SQL World",
"bookPrice": "550"
}
]
}
{
"update": [
{
"bookId": "6",
"bookTitle": "High Availability",
"bookPrice": "900"
}
],
"inject": [
{
"bookId": "1",
"bookTitle": "SQL World",
"bookPrice": "550"
}
]
}
the update is because the same value exists in sourceJson but has different values

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
im coding a guessing game but got an error [Answered]does anyone know how to fix this errorHow to use .NET 7 preview?I am trying to use .NET7 (Version: `7.0.100-preview.7.22377.5`) and I am using VS 2022 (latest previsource and targetSay I have two `Dictionary<string,JValue>` items. and I want to return a new `JsonConvert.SerializeOSending email SMTP[Answered]I am trying to send an email using SMTP. The backend works, and i can send emails via Swagger. But iExecute multiple steps with error checking [Answered]I have multiple steps that would be executed one by one with error checking after completing each stWindow opacity isn't being affected?I have a button click method over here that should set a window to opaque: ```cs private voiCoding winforms on ubuntu? is it possible now? if yes how?Can I work with winforms on ubuntu? I know winforms used to be limited to windows only, is it still System.Net.Sockets spookynessHello! : I've been tasked with updating our web app backend to .net 6 and I am almost done. HoweveINotifyDataErrorInfo helpWhen using the implementation for INotifyDataErrorInfo, is it possible when raising errors change evHow to pass value to parameter that require IEnumable string [Answered]Help me with the syntaxIs Array.Copy decent for copying sections of memory?I'm making a thing which relies pretty heavily on copying (not very large) sections of memory insideRelationship between Microsoft.AspNetCore.Identity.EntityFrameworkCore and AADHey guys, I read a bunch of Azure Active Directory documents and see that they recommend us not to sMSIX URI is inaccessible in AppinstallerHello, I have an Appinstaller and MSIX hosted in a public Google Cloud Storage bucket, meaning theyI'm having problems with a range of numbers and random generator (im new)I have an array of number (1-50, scaled), where one will be selected randomly and then every number EFCore Updating an item after mapping with AutomapperHey I am using Ef core to store some data in sqlite. I need to translate it to another object to shoIs it possible to allow an installer to downgrade dll's while not overwriting other app files?When creating an installer. (using Wix) Is there a way to have better control over the reinstall pro