First thing is to create a class structure to convert the JSON to. You can use
1.
‘Paste JSON As Classes’ Visual Studio Addin[
^]; or
2. A webtool like
JSON Utils[
^]
#2 above is my personal favourite as you have greater control over the code generated. Pasting your JSON into that website tool generates the following class structure:
public class TestRevisionHistory
{
[JsonProperty("test.createdBy")]
public string TestCreatedBy { get; set; }
[JsonProperty("test.createTimeStamp")]
public DateTime TestCreateTimeStamp { get; set; }
[JsonProperty("test.updatedBy")]
public string TestUpdatedBy { get; set; }
[JsonProperty("test.updatedTimeStamp")]
public DateTime TestUpdatedTimeStamp { get; set; }
}
public class TestIdentifiers
{
[JsonProperty("test.PlanId")]
public string TestPlanId { get; set; }
}
public class Headers
{
[JsonProperty("test.source")]
public string TestSource { get; set; }
[JsonProperty("test.agency")]
public string TestAgency { get; set; }
[JsonProperty("test.entity")]
public string TestEntity { get; set; }
[JsonProperty("test.revisionHistory")]
public TestRevisionHistory TestRevisionHistory { get; set; }
[JsonProperty("test.identifiers")]
public TestIdentifiers TestIdentifiers { get; set; }
}
public class MonitoringExaminer
{
[JsonProperty("examinerName")]
public string ExaminerName { get; set; }
[JsonProperty("examinerEmail")]
public string ExaminerEmail { get; set; }
[JsonProperty("examinerPhone")]
public string ExaminerPhone { get; set; }
}
public class PlanContact
{
[JsonProperty("ContactName")]
public string ContactName { get; set; }
[JsonProperty("ContactEmail")]
public string ContactEmail { get; set; }
[JsonProperty("eFileContactPhone")]
public string EFileContactPhone { get; set; }
}
public class TestLicensingContact
{
[JsonProperty("licensingContactName")]
public string LicensingContactName { get; set; }
[JsonProperty("licensingContactEmail")]
public string LicensingContactEmail { get; set; }
[JsonProperty("licensingContactPhone")]
public string LicensingContactPhone { get; set; }
}
public class FinancialContact
{
[JsonProperty("financialContactName")]
public string FinancialContactName { get; set; }
[JsonProperty("financialContactEmail")]
public string FinancialContactEmail { get; set; }
[JsonProperty("financialContactPhone")]
public string FinancialContactPhone { get; set; }
}
public class Instance
{
[JsonProperty("PlanId")]
public string PlanId { get; set; }
[JsonProperty("PlanName")]
public string PlanName { get; set; }
[JsonProperty("PlanType")]
public string PlanType { get; set; }
[JsonProperty("MailingState")]
public string MailingState { get; set; }
[JsonProperty("MailingZipCode")]
public string MailingZipCode { get; set; }
[JsonProperty("MailingAddress")]
public string MailingAddress { get; set; }
[JsonProperty("MailingCity")]
public string MailingCity { get; set; }
[JsonProperty("MonitoringExaminer")]
public MonitoringExaminer MonitoringExaminer { get; set; }
[JsonProperty("planContact")]
public PlanContact PlanContact { get; set; }
[JsonProperty("testLicensingContact")]
public TestLicensingContact TestLicensingContact { get; set; }
[JsonProperty("financialContact")]
public FinancialContact FinancialContact { get; set; }
}
public class Envelope
{
[JsonProperty("headers")]
public Headers Headers { get; set; }
[JsonProperty("triples")]
public IList<object> Triples { get; set; }
[JsonProperty("instance")]
public Instance Instance { get; set; }
[JsonProperty("attachments")]
public object Attachments { get; set; }
}
public class Response
{
[JsonProperty("envelope")]
public Envelope Envelope { get; set; }
}
Nest you need to map the raw JSON data to the class structure. Again, there are a few libraries out there:
1. Microsoft's own functions:
How to: Serialize and Deserialize JSON Data | Microsoft Docs[
^];
2.
fastJSON[
^]; or
3.
Json.NET - Newtonsoft[
^]
#3 is my personal favourite as it allows support for complex custom serialization. For this solution, I'll be using #3. You can add it to your project using NuGet:
NuGet Gallery | Json.NET 10.0.3[
^] and here is my helper class:
public static class JsonConverter
{
public static string FromClass<T>(T data, bool isEmptyToNull = false, JsonSerializerSettings jsonSettings = null)
{
string response = string.Empty;
if (!EqualityComparer<T>.Default.Equals(data, default(T)))
response = JsonConvert.SerializeObject(data, jsonSettings);
return isEmptyToNull ? (response == "{}" ? "null" : response) : response;
}
public static T ToClass<T>(string data, JsonSerializerSettings jsonSettings = null)
{
var response = default(T);
if (!string.IsNullOrEmpty(data))
response = jsonSettings == null
? JsonConvert.DeserializeObject<T>(data)
: JsonConvert.DeserializeObject<T>(data, jsonSettings);
return response;
}
}
Then to use with the above classes:
string rawJson = "{\"envelope\":{\"headers\":{\"test.source\":\"source system\", \"test.agency\":\"test\", \"test.entity\":\"Plan\", \"test.revisionHistory\":{\"test.createdBy\":\"admin\", \"test.createTimeStamp\":\"2017-08-09T00:00:00\", \"test.updatedBy\":\"admin\", \"test.updatedTimeStamp\":\"2017-08-09T00:00:00\"}, \"test.identifiers\":{\"test.PlanId\":\"10000001\"}}, \"triples\":[], \"instance\":{\"PlanId\":\"10000001\", \"PlanName\":\"The Health Plan\", \"PlanType\":\"Discount Plan\", \"MailingState\":\"CA \", \"MailingZipCode\":\"99999-1503\", \"MailingAddress\":\"Some Name LLP, 350 South Street Ave., 13th Floor\", \"MailingCity\":\"Los Angeles\", \"MonitoringExaminer\":{\"examinerName\":\"abc\", \"examinerEmail\":\"abc@test.ca.gov\", \"examinerPhone\":\"916-222-2222\"}, \"planContact\":{\"ContactName\":\"xyz\", \"ContactEmail\":\"xyz@Plan.com\", \"eFileContactPhone\":\"916-111-1111\"}, \"testLicensingContact\":{\"licensingContactName\":\"xyz\", \"licensingContactEmail\":\"xyz@test.ca.gov\", \"licensingContactPhone\":\"916-111-1111\"}, \"financialContact\":{\"financialContactName\":\"Jack Doe\", \"financialContactEmail\":\"xyz@Plan.com\", \"financialContactPhone\":\"916-111-1111\"}}, \"attachments\":null}}";
var result = JsonConverter.ToClass<Response>(rawJson);