Creating single Array of objects from multiple array objects in c#

I have a web API response like below and I need to move all objects into a single array in C# ASP.NET

[
   [
      {
         "id":23,
         "name":"John",
         "email":"<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e745156507e5a51535f5750105d5153">[email protected]</a>",
         "appointment_date":"tomorrow",
         "appointment_category":3,
         "time":"morning"
      },
      {
         "id":35,
         "name":"John",
         "email":"<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5ffdadddbf5d1dad8d4dcdb9bd6dad8">[email protected]</a>",
         "appointment_date":"tomorrow",
         "appointment_category":4,
         "time":"afternoon"
      }
   ],
   [
      {
         "id":17,
         "name":"Alex",
         "email":"Alex @domain.com",
         "appointment_date":"tomorrow",
         "appointment_category":3,
         "time":"morning"
      }
   ],
   [
      {
         "id":22,
         "name":"Bob",
         "email":"<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6a48984a682898b878f88c885898b">[email protected]</a>",
         "appointment_date":"tomorrow",
         "appointment_category":5,
         "time":"morning"
      }
   ]
]

I want to move all objects into single array. Like below
[
   {
      "id":23,
      "name":"John",
      "email":"<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97ddf8fff9d7f3f8faf6fef9b9f4f8fa">[email protected]</a>",
      "appointment_date":"tomorrow",
      "appointment_category":3,
      "time":"morning"
   },
   {
      "id":17,
      "name":"John",
      "email":"<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="703a1f181e30141f1d11191e5e131f1d">[email protected]</a>",
      "appointment_date":"tomorrow",
      "appointment_category":3,
      "time":"morning"
   },
   {
      "id":17,
      "name":"John",
      "email":"<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="246e4b4c4a64404b49454d4a0a474b49">[email protected]</a>",
      "appointment_date":"tomorrow",
      "appointment_category":3,
      "time":"morning"
   }
]

Please help me Thank u

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Not sure if you have deserialized the json already. But you can do it like this. Create 2 classes and deserialize with Newtonsoft.Json. Then use Linq with SelectMany to get a list of single object.

//deseralize the json
var list1 = JsonConvert.DeserializeObject<List<List<Class2>>>(json);

//select all the nested items
var list2 = list1.SelectMany(x => x).ToList();

The classes
public class Class1
{
    public List<Class1> list { get; set; }
}


public class Class2
{
    public int id { get; set; }
    public string name { get; set; }
    public string email { get; set; }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x