Tappy
Tappy

Reputation: 105

How to pass a complex JSON object through VB.net to call Rest API

I have a Rest API which I have to call from my vb.net code. In the Postman I pass the arguments in the Body as follows:

{
    "Search" : 
    {
        "EmpName" : "Rachel",
        "EmpID" : "1100320"

    },
    "IncludeAttributes" : ["EmpId", "EmpName","Department","Salary","ManagerId"]
}

My problem is that I am not sure how can I pass this kind of parameter through my call from VB.Net call. Here is what I have tried:

Dim searchParameters = New Dictionary(Of String, String)()
        searchParameters.Add("EmpName", "Rachel")
        searchParameters.Add("EmpID", "1100320")
        Dim jsonSearchParameters = 
                        JsonConvert.SerializeObject(searchParameters)
        Dim stringContent = New StringContent(jsonSearchParameters, Encoding.UTF8, "application/json")

And now I am clueless of how to pass IncludeAttributes part in the same object. Any help please?

Upvotes: 2

Views: 2982

Answers (2)

Tappy
Tappy

Reputation: 105

As cricket_007 suggested I tried Dictionary of string, object and it worked like a charm. For any one else coming here with the similar issue here is my piece of code, which worked:

Dim searchParameters = New Dictionary(Of String, String)()
searchParameters.Add("EmpId", EmpId)
searchParameters.Add("EmpName", "Rachel")

Dim includeAttributes() As String = {"EmpId", "Name", "Dept", "Manager", "Salary"}

Dim SearchAttributes As New Dictionary(Of String, Object)
SearchAttributes.Add("Search", searchParameters)
SearchAttributes.Add("IncludeAttributes", includeAttributes)

Dim jsonSearchParameters = JsonConvert.SerializeObject(SearchAttributes)

Upvotes: 1

theduck
theduck

Reputation: 2617

As @cricket_007 suggests a Dictionary(Of String, Object) would work:

Dim search = New Dictionary(Of String, String) From {
    {"EmpName", "Rachel"},
    {"EmpID", "1100320"}
}

Dim searchParameters = New Dictionary(Of String, Object) From {
    {"Search", search},
    {"IncludeAttributes", New String() {"EmpId", "EmpName", "Department", "Salary", "ManagerId"}}
}

Dim jsonSearchParameters = JsonConvert.SerializeObject(searchParameters)
Dim stringContent = New StringContent(jsonSearchParameters, Encoding.UTF8, "application/json")

Alternatively, create a class to hold the parameters:

Public Class Search
    Public Property EmpName As String
    Public Property EmpID As String
End Class

Public Class SearchParameters
    Public Property Search As Search
    Public Property IncludeAttributes As String()
End Class

and then fill the classes in with the appropriate data:

Dim search = New Search With
    {
        .EmpName = "Rachel",
        .EmpID = "1100320"
    }

Dim searchParameters = New SearchParameters With
    {
        .Search = search,
        .IncludeAttributes = New String() {"EmpId", "EmpName", "Department", "Salary", "ManagerId"}
    }

Dim jsonSearchParameters = JsonConvert.SerializeObject(searchParameters)
Dim stringContent = New StringContent(jsonSearchParameters, Encoding.UTF8, "application/json")

Upvotes: 1

Related Questions