Fred Kerber
Fred Kerber

Reputation: 187

How do I generate specific JSON output from VB.Net?

Being a VB.Net programmer using VB.Net 2015 community, I come across items in C# that I need to convert to VB, but this time I don't understand what I'm working with. The website service I'm consuming returns and expects JSON / JOBJECTS structures like:

var token = new {
    iss = PARTNERID,
    product = "twpemp",
    sub = "partner",
    siteInfo = new {
        type = "id",
        id = SITEID
    },
    exp = (Int32)DateTime.UtcNow.Add(new TimeSpan(0, 4, 30)).Subtract(new DateTime(1970, 1, 1)).TotalSeconds
};

An online converter converted this into:

Dim EPochTime = DateTime.UtcNow.Add(New TimeSpan(0, 4, 0)).Subtract(New DateTime(1970, 1, 1)).TotalSeconds
Dim Token = New With {
    Key .iss = AccNumber,
    Key .product = "twppartner",
    Key .sub = "partner",
    Key .siteInfo = New With {
        Key .type = "id",
        Key .id = Site},
    Key .exp = EPochTime
}

I need to dynamically create this type of structures because the "key name" and values change depending on what was returned and needs to be sent back. For example, depending on the siteid from above, the returning structure might have stuff like:

"Results": [
{
  "RecordNumber": 000001,
  "EmployeeCode": "0001",
  "FirstName": "John",
  "MiddleName": "A",
  "LastName": "Dow",
  "Designation": "Worker",
  "Home1": "Press",
},
{
  "RecordNumber": 000002,
  "EmployeeCode": "0002",
  "FirstName": "Jane",
  "MiddleName": "b",
  "LastName": "Dow",
  "Designation": "Helper",
  "Home1": "Office",
}
}

For the next client I submit a query for, and eventually need to update with might have:

"Results": [
{
  "RecordNumber": 12345,
  "EmployeeCode": "231",
  "FirstName": "Erick",
  "MiddleName": "G",
  "LastName": "Smith",
  "Department": "Electrial",
},
{
  "RecordNumber": 732456,
  "EmployeeCode": "853",
  "FirstName": "Fred",
  "MiddleName": "W",
  "LastName": "Kerber",
  "Department": "Electrial",
}
}

The difference between the two is one has "Department" and the other doesn't. This structure changes based on the siteID from the first call.

My main question is how do I create something like this dynamically in VB.NET, and secondarily, exactly what is this type of thing called? I'm calling it a structure for lack of better words.

Upvotes: 0

Views: 82

Answers (1)

Xavier J
Xavier J

Reputation: 4644

If you want a little more flexibility with outputting this stuff in JSON, there are two approaches:

1) You can use Dictionary<string,object> instead of dynamic types. With the dictionary approach, you can add (or exclude) properties at run-time. The JSON serializer will output in the same fashion as if you were serializing a dynamic type.

 var dict = new Dictionary<string, object>() { { "key1", "value1"} };
 dict["key2"] = DateTime.Now();
 dict["key3"] = 1234567;
 if (someCondition){
       dict["key4"] = new Dictionary<string, object>() { { "key5", "value5"}, { "key6", "value6"}};
 }

2) You can create a class that has ALL the available properties that the JSON structure might include. For optional properties which are numeric types, make them nullable:

public class Qwijibo 
{
    public int? RecordNumber {get;set;}
    public string EmployeeCode  {get;set;}
    public string FirstName {get;set;}
    public string MiddleName {get;set;}
    public string LastName {get;set;}
    public string Designation {get;set;}
    public string Home1 {get;set;}
    public string Department {get;set;
}

The above class may work in both the scenarios you presented. Properties you don't assign a value to will serialize as null in JSON. As long as whatever you're transmitting to doesn't get hung up on null values, you're good to go.

Upvotes: 1

Related Questions