Search code examples
arraysjsonvb.netjson.net

Problems with Json array


I'm writing a VB.Net application with an API to send values to a datasource. My JSON output is made with NewtonSoft.Json like the following:

{
    "Id": null,
    "Name": "Werkkaart_tests",
    "ExternalId": "Werkkaart_test",
    "Headers": null,
    "Rows": null,
    "NewRows": ["val 1, val 2, val 3, val 4"],
    "DeletedRows": null,
    "CompanyId": 1111,
    "IntegrationKey": "lsfjlsfjlsfj3987"
}

With a PUT request I can update the datasource. The model schema of the JSON is like this:

{
  "Id": "",
  "Name": "",
  "ExternalId": "",
  "Headers": [
    {
      "Name": "",
      "DisplayAt": ""
    }
  ],
  "Rows": [
    "Array[string]"
  ],
  "NewRows": [
    "Array[string]"
  ],
  "DeletedRows": [
    "Array[string]"
  ],
  "CompanyId": 0,
  "IntegrationKey": ""
}

The datasource has 4 columns named col1, col2, col3 and col4. To add new rows to the datasource I have to make an Array of String in NewRows.

When I send my JSON-string columns 1 till 4 are filled with the following values:

col1 col2 col3 col4
val 1 val 2

What I want is the following:

col1 col2 col3 col4
val1 val2 val3 val4

I tried placing single quotes around my values, but that doesn't work. I also tried the following:

"NewRows": ["val 1", "val 2", "val 3", "val 4"],

but the I get the following result:

col1 col2 col3 col4
val1
val2
val3
val4

Here's the vb code:

Dim src As New DSS.DataSource
Dim values As List(Of String) = New List(Of String)

values.Add("val 1") 
values.Add("val 2")
values.Add("val 3")

src.Name = "SourceName"
src.ExternalId = "Source_ExtID"

Dim newRowsString As String = String.Join(", ", values)
src.NewRows = New List(Of String) From {newRowsString} 

Dim json As String = JsonConvert.SerializeObject(src)
Console.WriteLine(json)

Solution

  • Note that the type of NewRows is denoted as "Array[String]". But you are providing a scalar string i.e. "val 1, val 2, val 3, val 4". You should provide the values as separate strings inside an array, so that NewRows is an array of arrays, i.e. the resulting json should be "NewRows" : [["val 1", "val 2", "val 3", "val 4"]].

    Each element in the outer array is a different row, each element of the inner array is a separate column within that particular row.