Read Data from Json into Excel

Ruffone
Ruffone used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rikin ShahMicrosoft Dynamics CRM Consultant

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
IT / Software Engineering Consultant
Top Expert 2016
Commented:
A couple of things.

First, that API returns a JSON data stream that looks like:

{
    "range": "1m",
    "data": [
        {
            "date": "2019-01-11",
            "open": 152.227,
            "high": 153.0435,
            "low": 150.8629,
            "close": 151.6396,
            "volume": 27023241,
            "unadjustedVolume": 27023241,
            "change": -1.5036,
            "changePercent": -0.982,
            "vwap": 151.7308,
            "label": "Jan 11",
            "changeOverTime": 0
        },
        {
            "date": "2019-01-14",
            "open": 150.2057,
            "high": 150.6239,
            "low": 148.5827,
            "close": 149.3594,
            "volume": 32439186,
            "unadjustedVolume": 32439186,
            "change": -2.2802,
            "changePercent": -1.504,
            "vwap": 149.3573,
            "label": "Jan 14",
            "changeOverTime": -0.015036969234949233
        },
        {
            "date": "2019-01-15",
            "open": 149.6282,
            "high": 152.7349,
            "low": 149.4091,
            "close": 152.4162,
            "volume": 28710324,
            "unadjustedVolume": 28710324,
            "change": 3.0569,
            "changePercent": 2.047,
            "vwap": 151.667,
            "label": "Jan 15",
            "changeOverTime": 0.0051213535250686625
        },
        {
            "date": "2019-01-16",
            "open": 152.4262,
            "high": 155.2142,
            "low": 152.3465,
            "close": 154.2783,
            "volume": 30569706,
            "unadjustedVolume": 30569706,
            "change": 1.862,
            "changePercent": 1.222,
            "vwap": 154.2783,
            "label": "Jan 16",
            "changeOverTime": 0.01740112740999053
        },
        {
            "date": "2019-01-17",
            "open": 153.5414,
            "high": 156.9866,
            "low": 152.6054,
            "close": 155.1943,
            "volume": 29821160,
            "unadjustedVolume": 29821160,
            "change": 0.916071,
            "changePercent": 0.594,
            "vwap": 154.3107,
            "label": "Jan 17",
            "changeOverTime": 0.023441765871184024
        },
        {
            "date": "2019-01-18",
            "open": 156.8273,
            "high": 157.2057,
            "low": 155.3144,
            "close": 156.1502,
            "volume": 33751023,
            "unadjustedVolume": 33751023,
            "change": 0.9559,
            "changePercent": 0.616,
            "vwap": 156.281,
            "label": "Jan 18",
            "changeOverTime": 0.029745528212947084
        },
        {
            "date": "2019-01-22",
            "open": 155.742,
            "high": 156.0606,
            "low": 151.9682,
            "close": 152.6453,
            "volume": 30393970,
            "unadjustedVolume": 30393970,
            "change": -3.505,
            "changePercent": -2.245,
            "vwap": 153.603,
            "label": "Jan 22",
            "changeOverTime": 0.006632172598714256
        },
        {
            "date": "2019-01-23",
            "open": 153.4916,
            "high": 154.4774,
            "low": 151.0521,
            "close": 153.2626,
            "volume": 23130570,
            "unadjustedVolume": 23130570,
            "change": 0.617352,
            "changePercent": 0.404,
            "vwap": 152.7788,
            "label": "Jan 23",
            "changeOverTime": 0.01070300897654696
        },
        {
            "date": "2019-01-24",
            "open": 153.4518,
            "high": 153.8202,
            "low": 151.0919,
            "close": 152.0478,
            "volume": 25441549,
            "unadjustedVolume": 25441549,
            "change": -1.2148,
            "changePercent": -0.793,
            "vwap": 152.0069,
            "label": "Jan 24",
            "changeOverTime": 0.002691908973645365
        },
        {
            "date": "2019-01-25",
            "open": 154.8159,
            "high": 157.4546,
            "low": 153.6609,
            "close": 157.0862,
            "volume": 33547893,
            "unadjustedVolume": 33547893,
            "change": 5.0384,
            "changePercent": 3.314,
            "vwap": 156.1717,
            "label": "Jan 25",
            "changeOverTime": 0.03591805834359883
        },
        {
            "date": "2019-01-28",
            "open": 155.1246,
            "high": 155.6623,
            "low": 153.0037,
            "close": 155.6324,
            "volume": 26192058,
            "unadjustedVolume": 26192058,
            "change": -1.4538,
            "changePercent": -0.925,
            "vwap": 154.6201,
            "label": "Jan 28",
            "changeOverTime": 0.026330852890669644
        },
        {
            "date": "2019-01-29",
            "open": 155.5827,
            "high": 157.4546,
            "low": 153.4518,
            "close": 154.0194,
            "volume": 41587239,
            "unadjustedVolume": 41587239,
            "change": -1.6131,
            "changePercent": -1.036,
            "vwap": 155.1149,
            "label": "Jan 29",
            "changeOverTime": 0.01569378974885181
        },
        {
            "date": "2019-01-30",
            "open": 162.5528,
            "high": 165.4404,
            "low": 159.5457,
            "close": 164.5442,
            "volume": 61109780,
            "unadjustedVolume": 61109780,
            "change": 10.5249,
            "changePercent": 6.833,
            "vwap": 162.5289,
            "label": "Jan 30",
            "changeOverTime": 0.08510046188462636
        },
        {
            "date": "2019-01-31",
            "open": 165.4005,
            "high": 168.2782,
            "low": 163.8572,
            "close": 165.7291,
            "volume": 40739649,
            "unadjustedVolume": 40739649,
            "change": 1.1849,
            "changePercent": 0.72,
            "vwap": 166.4004,
            "label": "Jan 31",
            "changeOverTime": 0.09291438384168771
        },
        {
            "date": "2019-02-01",
            "open": 166.2469,
            "high": 168.2583,
            "low": 165.2213,
            "close": 165.8088,
            "volume": 32668138,
            "unadjustedVolume": 32668138,
            "change": 0.079658,
            "changePercent": 0.048,
            "vwap": 166.4557,
            "label": "Feb 1",
            "changeOverTime": 0.09343997214447934
        },
        {
            "date": "2019-02-04",
            "open": 166.695,
            "high": 170.9219,
            "low": 166.5655,
            "close": 170.5186,
            "volume": 31495582,
            "unadjustedVolume": 31495582,
            "change": 4.7098,
            "changePercent": 2.84,
            "vwap": 169.6668,
            "label": "Feb 4",
            "changeOverTime": 0.12449914138523176
        },
        {
            "date": "2019-02-05",
            "open": 172.1217,
            "high": 174.3322,
            "low": 171.614,
            "close": 173.4361,
            "volume": 36101628,
            "unadjustedVolume": 36101628,
            "change": 2.9175,
            "changePercent": 1.711,
            "vwap": 173.2622,
            "label": "Feb 5",
            "changeOverTime": 0.14373883866747214
        },
        {
            "date": "2019-02-06",
            "open": 173.9041,
            "high": 174.8201,
            "low": 172.1148,
            "close": 173.4958,
            "volume": 28239591,
            "unadjustedVolume": 28239591,
            "change": 0.059744,
            "changePercent": 0.034,
            "vwap": 173.5179,
            "label": "Feb 6",
            "changeOverTime": 0.14413253530080533
        },
        {
            "date": "2019-02-07",
            "open": 171.6637,
            "high": 173.1971,
            "low": 169.6125,
            "close": 170.2099,
            "volume": 31741690,
            "unadjustedVolume": 31741690,
            "change": -3.2859,
            "changePercent": -1.894,
            "vwap": 170.9104,
            "label": "Feb 7",
            "changeOverTime": 0.12246339346714184
        },
        {
            "date": "2019-02-08",
            "open": 168.99,
            "high": 170.66,
            "low": 168.42,
            "close": 170.41,
            "volume": 23819966,
            "unadjustedVolume": 23819966,
            "change": 0.200085,
            "changePercent": 0.118,
            "vwap": 169.7869,
            "label": "Feb 8",
            "changeOverTime": 0.12378296962007282
        },
        {
            "date": "2019-02-11",
            "open": 171.05,
            "high": 171.21,
            "low": 169.25,
            "close": 169.43,
            "volume": 20993425,
            "unadjustedVolume": 20993425,
            "change": -0.98,
            "changePercent": -0.575,
            "vwap": 169.9464,
            "label": "Feb 11",
            "changeOverTime": 0.11732027781661258
        }
    ]
}

Open in new window


Notice that it is a nested structure, and at the top level there are two elements, "range" and "data".  The "data" element is a collection of nested nodes.

Also notice that you are referencing an element labeled "marketClose" but no such label exists in the JSON.  I suspect you are looking for "close"?

If so then use the following line to replace the one you are getting an error on.  This will look in the top level "data" element, get the first entry in that collection (1), and then get the "close" element value for that entry.

i = Json("data")(1)("close")

Open in new window


»bp
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Author

Commented:
That's a beautiful line of code right there
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Glad that was helpful.


»bp

Author

Commented:
Thanks Bill

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial