Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a JSon that I need to create an API for in C# from D365. Is there any packages that can help me with this in C#? I have never created an API before in C# so this is new to me. What do I need to put into a control in C#?

"d365EntityName": "account",
  "entityAlias": "account",
  "crmId": "0575ac39-a3c1-4d01-8cfa-4e52278c8665",
  "companyName": "AUTOLIV",
  "divisionId": null,
  "divisionName": null,
  "advisor": "",
  "bdRia": null,
  "bdRiaName": null,
  "billingLocationId": null,
  "billingLocationName": null,
  "clientGroupInt": null,
  "clientGroupName": null,
  "dateLost": null,
  "dba": "",
  "effectiveDate": null,
  "employeeBenefitsCoverageTypeInt": 867460000,
  "employeeBenefitsCoverageTypeName": "Group",
  "fein": "",
  "sicId": null,
  "sicName": null,
  "industryId": null,
  "industryName": null,
  "integrationMessage": "",
  "integrationStatus": "",
  "mainPhone": "",
  "numberOfEmployees": 23,
  "ownerName": "Tricia McCann",
  "regionInt": null,
  "regionName": null,
  "relationshipStatus": 3,
  "relationshipStatusName": "Green",
  "shippingLocationId": null,
  "shippingLocationName": null,
  "sourceId": "",
  "sourceSystem": null,
  "stateId": "99f9b75d-860b-ea11-a814-000d3a55d7ac",
  "stateName": "Arizona",
  "termedDate": null,
  "parentAccountId": "c39aaadf-dd6d-ea11-80db-005056912bbc",
  "parentAccountName": "DBA - St. Louis-MO (Centro Benefits Research LLC)",
  "companyTypeInt": 867460001,
  "companyTypeName": "Client",
  "brokerBooks": null,
  "products": null,
  "parentAccount": null,
  "name": null,
  "statusCodeInt": 1,
  "statusCodeName": "Active",
  "stateCodeInt": 0,
  "stateCodeName": "Active",
  "createdOn": "2023-07-10T09:22:08Z",
  "createdById": "c57e6623-2fbf-ed11-83ff-6045bdf05c47",
  "createdByName": "# D365 - Operational API User",
  "modifiedOn": "2023-11-17T07:40:19Z",
  "modifiedById": "c57e6623-2fbf-ed11-83ff-6045bdf05c47",
  "modifiedByName": "# D365 - Operational API User",
  "ownerId": "67f1f314-c74e-ea11-a813-000d3a55d7ac"


What I have tried:

C#
<pre>namespace Benchmarking.Services.Api.Models
{
    public class AccountDetails
    {
        [JsonProperty("App")]
        public App App { get; set; }

        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("Account Id")]
        public Guid AccountId { get; set; }

        [JsonProperty("Number of Employees")]
        public int NumberOfEmployees { get; set; }

        [JsonProperty("State Name")]
        public string StateName { get; set; }

        [JsonProperty("Industry Code")]
        public string IndustryCode { get; set; }

        [JsonProperty("Partner")]
        public string Partner { get; set; }

        [JsonProperty("User")]
        public User User { get; set; }

        [JsonProperty("Products")]
        public List<Product> Products { get; set; }
    }

    public partial class App
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }
    }

    public partial class Product
    {
        [JsonProperty("Name")]
        public string ProductName { get; set; }

        [JsonProperty("Product Type")]
        public string ProductType { get; set; }

        [JsonProperty("Product Plan Type")]
        public string ProductPlanType { get; set; }

        [JsonProperty("InsurancePlans")]
        public List<InsurancePlan> InsurancePlans { get; set; }

        [JsonProperty("PlanDetails")]
        public List<PlanDetail> PlanDetails { get; set; }

        [JsonProperty("PlanRates")]
        public List<PlanRate> PlanRates { get; set; }
    }

    public class InsurancePlan
    {
        public string PlanName { get; set; }
        public List<PlanDetail> PlanDetails { get; set; }
        public List<PlanRate> PlanRates { get; set; }
    }

    public partial class PlanDetail
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("Innetwork")]
        public string Innetwork { get; set; }

        [JsonProperty("Category")]
        public string Category { get; set; }
    }

    public partial class PlanRate
    {
        [JsonProperty("Tier Type")]
        public string TierType { get; set; }

        [JsonProperty("Rate Type")]
        public string RateType { get; set; }

        [JsonProperty("Transaction Currency Id")]
        public string TransactionCurrencyId { get; set; }

        [JsonProperty("Employee Rate Money")]
        public decimal? EmployeeRateMoney { get; set; }

        [JsonProperty("Employerate Money")]
        public decimal? EmployerateMoney { get; set; }

        [JsonProperty("Employee Rate Percent")]
        public decimal? EmployeeRatePercent { get; set; }

        [JsonProperty("Employer Rate Percent")]
        public decimal? EmployerRatePercent { get; set; }

        [JsonProperty("Contribution Unit")]
        public object? ContributionUnit { get; set; }

        [JsonProperty("Monthly Premium")]
        public decimal? MonthlyPremium { get; set; }
    }

    public partial class User
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("userid")]
        public string Userid { get; set; }
    }
Posted

1 solution

Here is a tool that will make it simple: JSON Utils: Generate C#, VB.Net, SQL Table and Java from JSON[^].

I have also written an article on working with JSON that list more tools and techniques: >Working with Newtonsoft.Json in C# & VB[^]
 
Share this answer
 
Comments
Computer Wiz99 25-Feb-24 21:42pm    
@Graeme_Grant Thanks. Will this just be for the controller.cs or is that a different code to write?
Graeme_Grant 25-Feb-24 21:46pm    
This is to address the issue of generating classes from JSON data and how to convert the raw JSON into usable classes. How you use it is then up to you.

FYI, the sample JSON that you posted is invalid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900