How format API complex model in c#

Binumon George 161 Reputation points
2023-01-09T13:38:09.003+00:00

Hi All,
I want to write one Post API to store data in database. API Structure should be same as below Json format. The requirement is below.

There is a organization with multiple branch. I want to collect information like organizations, branches, contact information in each branch etc. In branches there may be multiple or no contacts . In this case how i format my API model in asp.net c#

[{
"dataType": "organization",
"organizationData": {
"organizationNPI": "int",
"organizationName": "string",
"organizationTIN": "string",
"website": "string",

	"location": [{  
		"addressLine1": "string",  
		"addressLine2": "string",  
		"city": "string",  
		"state": "string",  
		"contact": [{  
			"firstName": "string",  
			"lastName": "string",  
			"middletName": "string",  
			"email": "string",  
			"phone": "string"  
		}, {  
			"firstName": "string",  
			"lastName": "string",  
			"middletName": "string",  
			"email": "string",  
			"phone": "string"  
		}, {  
			"firstName": "string",  
			"lastName": "string",  
			"middletName": "string",  
			"email": "string",  
			"phone": "string"  
		}]  

	}, {  
		"addressLine1": "string",  
		"addressLine2": "string",  
		"city": "string",  
		"state": "string"  

	}, {  
		"addressLine1": "string",  
		"addressLine2": "string",  
		"city": "string",  
		"state": "string"  

	}],  
	"specialities": {  
		"speciality1": "string",  
		"speciality2": "string"  
	},  
	"languages": {  
		"language1": "string",  
		"language2": "string",  
		"language3": "string"  
	}  

}  

}]

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
367 questions
{count} votes

Accepted answer
  1. Michael Taylor 56,441 Reputation points
    2023-01-09T16:50:45.427+00:00

    Are you asking for the C# structure to back the JSON you gave? Each curly brace represents a JSON object which maps to a C# class. Square brackets are arrays in JSON so they map to either an array or IEnumerable<T> in C#. Everything else is just simple primitive mappings.

       public class Organization  
       {  
          public string dataType { get; set; }  
          public OrganizationData organizationData { get; set; }  
       }  
         
       public class OrganizationData  
       {  
          public int organizationNPI { get; set; }  
          public string organizationName { get; set; }  
          public string organizationTIN { get; set; }  
          public string website { get; set; }  
          public IEnumerable<Location> location { get; set; }  
         
       }  
         
       public class Location  
       {  
          public string addressLine1 { get; set; }  
          public string addressLine2 { get; set; }  
          public string city { get; set; }  
          public string state { get; set; }  
         
          public IEnumerable<Contact> contact { get; set; }  
       }  
         
       public class Contact  
       {  
          public string firstName { get; set; }  
          public string lastName { get; set; }  
          public string middleName { get; set; }  
          public string email { get; set; }  
          public string phone { get; set; }  
       }  
    

    Note that if this is a REST API then most likely they provide an Open API spec for it. In that case you don't need to generate this stuff by hand. Visual Studio can auto-generate the client and models given an Open API definition. You should go that route instead.

    Also, the specialties and languages looks like an open-ended JSON object meaning the field names vary by the data being returned. If that is true then this cannot be directly done as properties on a class. However if literally the field names are language1, language2 then you can follow the pattern I showed where specialty and language are classes with the given properties. Then add a property of each of these types to the Location(??) type if that is what they relate to.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,976 Reputation points
    2023-01-09T21:29:33.55+00:00

    typically json mapped objects are converted to dictionaries:

     public Dictionary<string, string> specialities {get; set;}   
     public Dictionary<string, string> languages {get; set;}   
       
    
    0 comments No comments

  2. Lan Huang-MSFT 30,166 Reputation points Microsoft Vendor
    2023-01-10T03:03:36.093+00:00

    Hi @Binumon George , I think you are trying to deserialize complex JSON objects using C#. You can try newtonsoft.json package. You can refer to the code below:

    // use like
    var rootObj = JsonConvert.DeserializeObject&lt;RootObject&gt;(jsonResponse);
    foreach (var row in rootObj.rows)
    {
        foreach (var element in row.elements)
        {
            Console.WriteLine(element.distance.text);
        }
    }
    
    // you might want to change the property names to .Net conventions
    // use [JsonProperty] to let the serializer know the JSON names where needed
    public class Distance
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    
    public class Duration
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    
    public class Element
    {
        public Distance distance { get; set; }
        public Duration duration { get; set; }
        public string status { get; set; }
    }
    
    public class Row
    {
        public List&lt;Element&gt; elements { get; set; }
    }
    
    public class RootObject
    {
        public List&lt;string&gt; destination_addresses { get; set; }
        public List&lt;string&gt; origin_addresses { get; set; }
        public List&lt;Row&gt; rows { get; set; }
        public string status { get; set; }
    }
    

    You can also use dictionaries. For example:

    public class Rootobject
        {
            public Dictionary&lt;string, DataObject&gt; data { get; set; }
            public string type { get; set; }
            public string version { get; set; }
        }
        public class DataObject
        {
            public int id { get; set; }
            public string key { get; set; }
            public string name { get; set; }
            public string title { get; set; }
        }
       var obj = JsonConvert.DeserializeObject&lt;Rootobject&gt;(jsonResponse);
    

    Best regards, Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.