wpcoder
wpcoder

Reputation: 447

Convert CSV File to JSON

I want to post CURL command which accepts JSON Format. I searched internet and couldn't find a vb.net code how to convert CSV file into JSON format in order to post to API. Any advice appreciated.

Here is the code I am using to post to API. This question is unique as it asks how to use VB.Net 2013

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim wHeader As WebHeaderCollection = New WebHeaderCollection()
    wHeader.Clear()
    wHeader.Add("X-Appery-Database-Id: xxxxxxxxxxx")
    wHeader.Add("X-Appery-Session-Token:" & token)

    Dim sUrl As String = "https://api.appery.io/"
    'sUrl = System.Net.WebUtility.UrlEncode(sUrl)
    Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(sUrl), HttpWebRequest)

    wRequest.ContentType = "application/json"

    wRequest.Headers = wHeader
    wRequest.Method = "POST"  

    Dim stream = wRequest.GetRequestStream()
    stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
    stream.Close()

    Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)
    Dim sResponse As String = ""

    Using srRead As New StreamReader(wResponse.GetResponseStream())
       sResponse = srRead.ReadToEnd()
       Console.Write(sResponse)
       MsgBox(sResponse.ToString())

    End Using
End Sub

Thank you

Upvotes: 1

Views: 3580

Answers (1)

I have no idea what the data looks like to be able to to give a more applicable example. So using random data, the starting csv:

Name,StockNum,Color,OnHand,Price,ItemDate
turpis Nullam sagitt,K94-ZS89,Black,1,8.71,1/12/2017 12:00:00 AM
suscipit eget,Z25-XQKU,Topaz,0,14.48,1/14/2017 12:00:00 AM

Then, a class to drive it:

Public Class Something
    Public Property Name As String
    Public Property StockNum As String
    Public Property Color As String
    Public Property OnHand As Integer
    Public Property Price As Decimal
    Public Property ItemDate As DateTime

    Public Sub New()

    End Sub
End Class

Reading in the CSV and converting is then simple using JSON.NET and CSVHelper:

Dim things As IEnumerable(Of Something)
Dim jstr As String

Using sr As New StreamReader("C:\Temp\Json\CSVTOJson.csv"),
    csv As New CsvReader(sr)

    things = csv.GetRecords(Of Something)()
    jstr = JsonConvert.SerializeObject(things)
End Using

Note that this uses IEnumerable (but you can use a list or array instead). This makes it economical and perhaps faster especially if there are many records. They wont actually be loaded into your app, just passed to JSON.NET to read and serialize them.

Result:

[{
    "Name": "turpis Nullam sagitt",
    "StockNum": "K94-ZS89",
    "Color": "Black",
    "OnHand": 1,
    "Price": 8.71,
    "ItemDate": "2017-01-12T00:00:00"
}, {
    "Name": "suscipit eget",
    "StockNum": "Z25-XQKU",
    "Color": "Topaz",
    "OnHand": 0,
    "Price": 14.48,
    "ItemDate": "2017-01-14T00:00:00"
}, {
    "Name": "Proin faucibus arcu",
    "StockNum": "F64-WS5X",
    "Color": "Topaz",
    "OnHand": 6,
    "Price": 12.83,
    "ItemDate": "2017-01-18T00:00:00"
}
...
]

Upvotes: 1

Related Questions