The
payload
should be a class of properties, then you can encode to a JSON object that will generate a string for the actual payload.
One solution is to download a sample/example JSON payload, then use a tool like chat GPT to convert the JSON to VB class(es):
Here is my input:
convert this JSON to VB.NEt classes. Use Camel case and JsonProperty attribute. now use the sample data and show how to convert from class to JSON.
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
And here are the classes generated:
Imports Newtonsoft.Json
Public Class Batter
<JsonProperty("id")>
Public Property Id As String
<JsonProperty("type")>
Public Property Type As String
End Class
Public Class Topping
<JsonProperty("id")>
Public Property Id As String
<JsonProperty("type")>
Public Property Type As String
End Class
Public Class Donut
<JsonProperty("id")>
Public Property Id As String
<JsonProperty("type")>
Public Property Type As String
<JsonProperty("name")>
Public Property Name As String
<JsonProperty("ppu")>
Public Property Ppu As Decimal
<JsonProperty("batters")>
Public Property Batters As New Dictionary(Of String, List(Of Batter))
<JsonProperty("topping")>
Public Property Topping As List(Of Topping)
End Class
and the sample code:
Imports Newtonsoft.Json
Module Module1
Sub Main()
Dim donutObject As New Donut With {
.Id = "0001",
.Type = "donut",
.Name = "Cake",
.Ppu = 0.55D,
.Batters = New Dictionary(Of String, List(Of Batter)) From {
{"batter", New List(Of Batter) From {
New Batter With {.Id = "1001", .Type = "Regular"},
New Batter With {.Id = "1002", .Type = "Chocolate"},
New Batter With {.Id = "1003", .Type = "Blueberry"},
New Batter With {.Id = "1004", .Type = "Devil's Food"}
}}
},
.Topping = New List(Of Topping) From {
New Topping With {.Id = "5001", .Type = "None"},
New Topping With {.Id = "5002", .Type = "Glazed"},
New Topping With {.Id = "5005", .Type = "Sugar"},
New Topping With {.Id = "5007", .Type = "Powdered Sugar"},
New Topping With {.Id = "5006", .Type = "Chocolate with Sprinkles"},
New Topping With {.Id = "5003", .Type = "Chocolate"},
New Topping With {.Id = "5004", .Type = "Maple"}
}
}
Dim jsonStr As String = JsonConvert.SerializeObject(donutObject, Formatting.Indented)
Console.WriteLine(jsonStr)
End Sub
End Module
UPDATE
My ChatGPT input:
convert this JSON to VB.NEt classes. Use Camel case and JsonProperty attribute. now use the sample data and show how to convert from class to JSON.
{
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [{
"instruction": "Buy",
"quantity": 15,
"instrument": {
"symbol": "XYZ",
"assetType": "EQUITY"
}
}]
}
1. Classes generated:
Imports Newtonsoft.Json
Public Class Order
<JsonProperty("orderType")>
Public Property OrderType As String
<JsonProperty("session")>
Public Property Session As String
<JsonProperty("duration")>
Public Property Duration As String
<JsonProperty("orderStrategyType")>
Public Property OrderStrategyType As String
<JsonProperty("orderLegCollection")>
Public Property OrderLegCollection As List(Of OrderLeg)
End Class
Public Class OrderLeg
<JsonProperty("instruction")>
Public Property Instruction As String
<JsonProperty("quantity")>
Public Property Quantity As Integer
<JsonProperty("instrument")>
Public Property Instrument As Instrument
End Class
Public Class Instrument
<JsonProperty("symbol")>
Public Property Symbol As String
<JsonProperty("assetType")>
Public Property AssetType As String
End Class
2. Sample code generated:
Imports Newtonsoft.Json
Module Program
Sub Main()
Dim orderLeg As New OrderLeg With {
.Instruction = "Buy",
.Quantity = 15,
.Instrument = New Instrument With {
.Symbol = "XYZ",
.AssetType = "EQUITY"
}
}
Dim order As New Order With {
.OrderType = "MARKET",
.Session = "NORMAL",
.Duration = "DAY",
.OrderStrategyType = "SINGLE",
.OrderLegCollection = New List(Of OrderLeg) From {
orderLeg
}
}
Dim json As String = JsonConvert.SerializeObject(order, Formatting.Indented)
Console.WriteLine(json)
End Sub
End Module
I then created a console app, added the code, changed the module name to
Module1
to match the filename and ran. 100% successful. You will want to remove the
Formatting.Indented
when adding to your own code.