C
C#2y ago
malkav

Creating an Array of strings from a stringified JSON

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
5 Replies
DaVinki
DaVinki2y ago
I think you should consider creating objects out of the json source and target arrays instead of just getting string arrays out of them
Cisien
Cisien2y ago
What are you expecting in the resulting array?
malkav
malkav2y ago
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]);
Cisien
Cisien2y ago
Create an object with two properties, sourceJson, TargetJson that are arrays of that dictionary Serialize to that
malkav
malkav2y ago
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