0

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?

 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"
 }
 }
 ]
 }
 ]
 }
asked Oct 5, 2022 at 19:02
2
  • 2
    You could create a DTO class, parse the JSON into an instance of that class and then use LINQ to filter out unwanted objects in the list Commented Oct 5, 2022 at 19:09
  • Take a look at here stackoverflow.com/questions/23645034/… Commented Oct 5, 2022 at 19:34

1 Answer 1

2

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();
answered Oct 5, 2022 at 19:30
5
  • Code only answer. Commented Oct 5, 2022 at 19:32
  • @PoulBak "I am trying to take all the "interest" values, from all the sessions and all the events but if the event type = "search". This is my answer Commented Oct 5, 2022 at 19:34
  • I don't need an explanation, but OP and later searchers might. Commented Oct 5, 2022 at 19:37
  • Please mark as solution if this is your answer @user1428019 Commented Oct 5, 2022 at 19:42
  • @PoulBak I added some description Commented Oct 5, 2022 at 19:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.