We help IT Professionals succeed at work.

Read Data from Json into Excel

Ruffone
Ruffone asked
on
121 Views
Last Modified: 2019-02-12
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

Comment
Watch Question

Rikin ShahMicrosoft Dynamics CRM Consultant
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
That's a beautiful line of code right there
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Glad that was helpful.


ยปbp

Author

Commented:
Thanks Bill

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions