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

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.
Al230762Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
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
Al230762Author 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSON

From novice to tech pro — start learning today.