0

I'm receiving a JSON payload which has nested Lists. Can LINQ be used to flatten the structure and pull out the nested lists into a denormalized form?

I have a strong database background so that'd why I'm calling it denormalized form of the JSON data.

I've use LINQ before but not using these deep type of structures.

I've tried using the LINQ fluent method but I can;t seem to get to the nested Lists


        public class Exampleobject
        {
            public string result { get; set; }
            public Propertydata propertyData { get; set; }
        }

        public class Propertydata
        {
            public List<Buildings> building { get; set; }           
        }



        public class Buildings
        {
            public string itemId { get; set; }
            [JsonProperty("type")]
            public string buildingType { get; set; }
            public string buildingTypeCode { get; set; }
            public string buildingTypeDescription { get; set; }            

            [JsonProperty("floors")]
            public List<floorInvolved> floorsInvolved { get; set; }            
        }

        public class floorInvolved
        {
            public string InvolvedId { get; set; }
            public List<FRole> roles { get; set; }
        }

        public class FRole
        {
            public string code { get; set; }
            public string description { get; set; }
        }                     


Sample Data:


{
"result": "200 OK",

"propertyData": {

"building": [


{
"itemId": "9A85B1CCBD65C1F2",
"type": "V",
"buildingTypeCode": "02",
"buildingTypeDescription": "mixed space",

"floors": [

{
"InvolvedId": "04",

"roles": [

{
"code": "OFF",
"description": "Office space"
},

{
"code": "APT",
"description": "Apartment"
},

{
"code": "STG",
"description": "Storage"
}
]
},
{
"InvolvedId": "05",

"roles": [

{
"code": "OFF",
"description": "Office space"
},


]
}
],

}
]
}
}
I'm trying to get the building bubbled up with the details like this:
ID                Description  Floor  Role
9A85B1CCBD65C1F2  mixed space    04   Office space, Apartment, Storage
9A85B1CCBD65C1F2  mixed space    05   Office space

I load the json data like so
var resulting = JsonConvert.DeserializeObject<Exampleobject>(rawjson);
2
  • 2
    SelectMany in the extension method syntax, or from x in xs from y in x.ys ... in the query syntax. Commented Apr 11, 2019 at 13:49
  • I've played with the SelectMany and I still cannot get close to an answer on how to use LINQ correctly in this situation Commented Apr 11, 2019 at 17:40

1 Answer 1

2

Using query syntax:

var result =
    from building in resulting.propertyData.building
    from floor in building.floorsInvolved
    select $"{building.itemId} {building.buildingTypeDescription} " +
        $"{floor.InvolvedId} " +
        $"{string.Join(", ", floor.roles.Select(role => role.description))}";

Or alternatively (and perhaps slightly more readably):

var result =
    from building in resulting.propertyData.building
    from floor in building.floorsInvolved
    let roles = floor.roles.Select(role => role.description)
    select $"{building.itemId} {building.buildingTypeDescription} " +
        $"{floor.InvolvedId} {string.Join(", ", roles)}";

Using the extension method syntax:

 var result = resulting.propertyData.building
    .SelectMany(building => building.floorsInvolved
        .Select(floor => $"{building.itemId} {building.buildingTypeDescription} " +
            $"{floor.InvolvedId} " +
            $"{string.Join(", ", floor.roles.Select(role => role.description))}"));
Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful -- works like a charm and I finally have some insight on to proceed with using LINQ on this data. I owe you many beers!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.