Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# effective way to get JSON property value from nested JSON response

I am getting a HTTP response as JSON string. I am converting the response as JObject.
Response contains "sessions" node and it contains more sessions value
each sessions contains "events" with one or multiple "events"
each events contains "Type" and "ArbitaryData"
ArbitaryData contains "interest".

I am trying to take all the "interest" values, from all the sessions and all the events but if the event type = "search".

what’s the effective way to do that other than doing for loop?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 JObject json = JObject.Parse(guestJsonResponse);
 var sessions = json.SelectToken("sessions");
 var events = sessions.SelectTokens("events");

Below is my JSON string response example

  {
  "firstName":"fn",
  "lastName":"ln",
   "gender":"male",
  "sessions":[
  {
     "currency":"USD",
     "events":[
        {
           "type":"SEARCH",
           "status":"PROCESSED",
           "arbitraryData":{
              "interest":"Health"
           }
        },
        {
           "type":"CHECK",
           "status":"PROCESSED",
           "arbitraryData":{
              "interest":"Dental"
           }
        }
     ]
  },
  {
     "currency":"USD",
     "events":[
        {
           "type":"SEARCH",
           "status":"PROCESSED",
           "arbitraryData":{
              "interest":"Health"
           }
        },
        {
           "type":"CHECK",
           "status":"PROCESSED",
           "arbitraryData":{
              "interest":"Dental"
           }
        }
     ]
  },
  {
     "currency":"USD",
     "events":[
        {
           "type":"SEARCH",
           "status":"PROCESSED",
           "arbitraryData":{
              "interest":"Health"
           }
        },
        {
           "type":"CHECK",
           "status":"PROCESSED",
           "arbitraryData":{
              "interest":"Dental"
           }
        }
     ]
  }
   ]
     }

>Solution :

This code will return all the "interest" values, from all the sessions where the event type = "SEARCH"

List<string> interests = ((JArray)JObject.Parse(json)["sessions"])
                              .SelectMany(i => ((JArray)i["events"]))
                              .Where(x => (string)x["type"] == "SEARCH")
                              .Select(x => (string)x["arbitraryData"]["interest"])
                              .ToList();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading