Link to home
Start Free TrialLog in
Avatar of David Rudlin
David RudlinFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

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
ASKER CERTIFIED SOLUTION
Avatar of David Rudlin
David Rudlin
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial