I am trying to find a more elegant solution than foreach
loops for the following problem.
Sample JSON Structure:
[{
"OpportunityId": "ABC-123-XYZ",
"Status": "Active",
"Milestones": {
"Milestone": [
{
"Name": "Award",
"Date": "8/27/2021"
}
]
},
"Staff": {
"Pocs": [
{
"Role": "Development Lead",
"Name": "Doe, John",
"Email": "[email protected]"
},
{
"Role": "Capture Manager",
"Name": "Doe, Jane",
"Email": "[email protected]"
}
]
}
}]
I can get the data I want using the following:
string json = [see above];
JArray? obj = JsonConvert.DeserializeObject<JArray>(json);
foreach (var single in obj)
{
JToken? pocs = single.Value<JToken>("Staff")?.Value<JToken>("Pocs");
if (pocs != null)
{
foreach(var poc in pocs)
{
if (poc.Value<string>("Role") == "Capture Manager")
{
[Do something with the resulting JToken/values]
}
}
}
}
I feel like there has to be some Linq to help me here, but everything I’ve tried thus far has been unsuccessful. I have no control over the JSON input, I just need to deal with it as is. Any help would be greatly appreciated!
3
Answers
As mentioned by @gunr2171 in the comment, you can work with JSON Path.
The above JSON Path:
$
– From root element...Staff
– Deep scan the element node with theStaff
field..Pocs
– Access to the dot-notatedPocs
child.[?(<expression>)]
– Filter expression.@.Role == 'Capture Manager'
– Current node with the filter expression that matches theRole
field value equal to "Capture Manager".Reference:
Working with System.Linq and Newtonsoft.Json.Linq.
you can try this
You could always create a POCO and deserialize the JToken to that class object. This would allow you to create an array of that POCO from the JToken and use regular linq statements to work with it. It would take a bit more effort, but it might be easier to work with.