How to extract parent values and all child values from deserialized JSON, one record per child value, to a List<T>?

Al230762
Al230762 used Ask the Experts™
on
How do I extract parent values and all child values from deserialized JSON, one record per child value, to a List<T>?

Sample JSON:  json_sample.txt

So from the attached sample JSON we wish to add the following records to the  list:
Record 1:  aen: Y456789, studentID: 1016, need: MMM, rank: 1
Record 2:  aen: Y456789, studentID: 1016, need: PPP, rank: 2

All records with "needs : null" to be ignored and all "need": "Not Specified" to be ignored. Sample code below:



      CTFNeedDataBatch =
                           (from people in myJSON.content
                            select new NeedCTF
                            {
                                studID = people.StudentBasic.studentId.ToString(),
                                AEN= people.StudentBasic.aen.ToString(),
                                Code = (people.StudentSensitive.needs == null ? people.StudentSensitive.needs[0].need.ToString() : ""),
                                Priority = (people.StudentSensitive.needs == null ? people.StudentSensitive.needs[0].rank.ToString() : ""),
                            }).Where(w => w.Code != "").ToList();


The problem with this code is we are not getting any values from the "needs" element and even if we were, I doubt we would be getting more than one record from an element that has more than one need.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

I am writing from my phone and i do not have a pc near to test anything, but i do see an issue in your code here:

Code = (people.StudentSensitive.needs == null ? people.StudentSensitive.needs[0].need.ToString() : "")

Open in new window


Esdentially you say, "if the needs is null then get the first need to string, else get an empty string. You should have it like this :

Code = (people.StudentSensitive.needs != null ? people.StudentSensitive.needs[0].need.ToString() : "")

Open in new window


Giannis
Commented:
Thank you Giannis - at least that's a response and it did help with the 'not null' syntax.

However, we have now answered the question ourselves.

Historically it seems the response on this forum to JSON deserializing ToList questions has been very poor so I think more expertise is needed in this area.

Here is the code that works:

                   string currAEN = "";
                     string currStudID ="";
                    foreach (var subItem in myJSON.content.Where(w => w.StudentSensitive.senNeeds != null))
                    {
                        currAEN = subItem.StudentBasic.upn.ToString();
                  currStudID = subItem.StudentBasic.studentId.ToString();
                       
                            CTFNeedDataBatch =
                                       (from needs in subItem.StudentSensitive.senNeeds.Where(w => w.need != "Not Specified")                                      
                                          select new NeedCTF
                                    {
                                        studID = currStudID ,
                                        AEN= currAEN,
                                        Code = (needs.need != null ? needs.need.ToString() : ""),
                                        Priority = needs.rank.ToString(),

                            }).Where(w => w.Code != "Not Specified").ToList();

                    }


...and produces the following sample list items:
Record 1:  aen: Y456789, studentID: 1016, need: MMM, rank: 1
Record 2:  aen: Y456789, studentID: 1016, need: PPP, rank: 2

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial