Avatar of Ruffone
Ruffone

asked on 

Read Data from Json into Excel

I am trying to read data into an Excel sheet with the code below but I get a type mismatch error. I get the data as confirm with the message box

    Set myrequest = CreateObject("WinHttp.WinHttpRequest.5.1")
    myrequest.Open "Get", "https://api.iextrading.com/1.0/stock/aapl/chart/dynamic"
    myrequest.Send

    Dim Json As Object
    Set Json = JsonConverter.ParseJson(myrequest.ResponseText)
    
    'MsgBox (myrequest.ResponseText)

        i = Json(1)("marketClose") <---- Error
        ws.Range(Cells(n, 2), Cells(n, 2)) = i

Open in new window

Visual Basic.NETVB ScriptVBA

Avatar of undefined
Last Comment
Ruffone
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

Check following code snippet to create an excel file from JSON-

    Using clientjs = New HttpClient()
        Dim responseJSON As HttpResponseMessage = clientjs.GetAsync(url).Result
        responseJSON.EnsureSuccessStatusCode()
        Dim responseBody = responseJSON.Content.ReadAsStringAsync().Result
        Dim data = JsonConvert.DeserializeObject(responseBody)("value")

        Using client = New HttpClient()

            Using formData = New MultipartFormDataContent()
                Dim fileFormat = "xlsx"
                formData.Add(New StringContent("Test"), "FileName")
                formData.Add(New StringContent(fileFormat), "FileFormat")
                formData.Add(New StringContent(JsonConvert.SerializeObject(data)), "Data")
                Dim response = client.PostAsync(webapiurl, formData).Result

                If Not response.IsSuccessStatusCode Then
                    MessageBox.Show("Invalid response.")
                    Return
                End If

                Dim tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())

                If Not Directory.Exists(tempPath) Then
                    Directory.CreateDirectory(tempPath)
                End If

                Dim tempFilePath = Path.Combine(tempPath, String.Format("{0}.{1}", "Test", fileFormat))

                Using newFile = File.Create(tempFilePath)
                    response.Content.ReadAsStreamAsync().Result.CopyTo(newFile)
                End Using

                Process.Start(tempFilePath)
            End Using
        End Using
    End Using

Open in new window

Avatar of Ruffone
Ruffone

ASKER

Thanks Rikin,

I just looked at the code in Tim Hall's JsonConverter for the first time to see if it had SerializeObject and it doesn't. I am just trying to use a little VBA on the back of Excel
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ruffone
Ruffone

ASKER

That's a beautiful line of code right there
Avatar of Bill Prew
Bill Prew

Glad that was helpful.


»bp
Avatar of Ruffone
Ruffone

ASKER

Thanks Bill
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo