Click here to Skip to main content
15,887,300 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can't seem to create a JSON string that i can pass to a put request and I can't create a array inside an json object. Please help

What I have tried:

VB
Dim payload As Dictionary(Of String, String) = New Dictionary(Of String, String)()
payload.Add("session", "AM")
payload.Add("complexOrderStrategyType", "NONE")
payload.Add("price", "1000")
payload.Add("orderType", "LIMIT")
payload.Add("duration", "DAY")
payload.Add("orderStrategyType", "Single")
payload.Add("specialInstruction", "ALL_OR_NONE")
Dim orderLegCollection As Dictionary(Of String, String) = New Dictionary(Of String, String)(payload)
orderLegCollection.Add("instruction", "BUY")
orderLegCollection.Add("quantity", "1")
orderLegCollection.Add("quantityType", "SHARES")

Dim instrument As Dictionary(Of String, String) = New Dictionary(Of String, String)(orderLegCollection)
instrument.Add("symbol", "MSFT")
instrument.Add("assetType", "EQUITY")
Dim strPayload As String = JsonConvert.SerializeObject(payload)
Dim c As HttpContent = New StringContent(strPayload, Encoding.UTF8, "application/json")

Dim t = Task.Run(Function() PostURI(u, c))
t.Wait()
Posted
Updated 23-Jul-23 0:54am
v2

1 solution

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:
VB.NET
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:
VB.NET
Imports Newtonsoft.Json

Module Module1
    Sub Main()
        ' Creating a sample Donut object with data
        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"}
            }
        }

        ' Converting the Donut object to JSON
        Dim jsonStr As String = JsonConvert.SerializeObject(donutObject, Formatting.Indented)

        ' Displaying the JSON string
        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:
VB.NET
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:
VB.NET
Imports Newtonsoft.Json

Module Program
    Sub Main()
        ' Sample data
        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
            }
        }

        ' Convert to JSON
        Dim json As String = JsonConvert.SerializeObject(order, Formatting.Indented)

        ' Print the JSON
        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.
 
Share this answer
 
v4
Comments
John Robert Connolly 23-Jul-23 13:20pm    
Thanks for the help!!!
I am having issues creating the json string...the string form I need is:

{
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": "Buy",
"quantity": 15,
"instrument": {
"symbol": "XYZ",
"assetType": "EQUITY"
}
}
]
}
I have converted it to a class but I am stuck putting it together into the correct json string
Thisi what I am getting:

"{" & vbCrLf & " ""orderType"": ""SEAMLESS""," & vbCrLf & " ""session"": ""AM""," & vbCrLf & " ""duration"": ""DAY""," & vbCrLf & " ""orderStrategyType"": ""Single""," & vbCrLf & " ""orderLegCollection"": [" & vbCrLf & " {" & vbCrLf & " ""instruction"": ""BUY""," & vbCrLf & " ""quantity"": 0," & vbCrLf & " ""instrument"": null" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""instruction"": null," & vbCrLf & " ""quantity"": 1," & vbCrLf & " ""instrument"": null" & vbCrLf & " }" & vbCrLf & " ]" & vbCrLf & "}"
Graeme_Grant 23-Jul-23 17:26pm    
Worked for me. See the new updated section in my answer.
John Robert Connolly 23-Jul-23 13:29pm    
This iswhat i have so far:
Dim donutObject As New PlaceOrderHope With {
.orderType = "SEAMLESS",
.session = "AM",
.duration = "DAY",
.orderStrategyType = "Single",
.orderLegCollection = New List(Of OrderLegCollection) From {
New OrderLegCollection With {.instruction = "BUY"},
New OrderLegCollection With {.quantity = 1},
.Instrument = New List(Of Instrument) From {
New Instrument With {.symbol = "MSFT"},
New Instrument With {.assetType} = "EQUITY"}
}
}}
John Robert Connolly 23-Jul-23 13:38pm    
This is my classes
Public Class Instrument
Public Property symbol As String
Public Property assetType As String
End Class

Public Class OrderLegCollection
Public Property instruction As String
Public Property quantity As Integer
Public Property instrument As Instrument
End Class

Public Class PlaceOrderHope
Public Property orderType As String
Public Property session As String
Public Property duration As String
Public Property orderStrategyType As String
Public Property orderLegCollection As List(Of OrderLegCollection)
End Class
John Robert Connolly 24-Jul-23 6:33am    
YOUR AWESOMW MAN!!

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