I am getting a json response from an api and parse it to update in excel. Below is the code. I am not able to parse further to get the price info.
Dim strResult As String
Dim objHTTP As Object
Dim URL As String
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
URL = "https://bitbns.com/order/getTickerAll"
objHTTP.Open "GET", URL, False
objHTTP.Send
Set JSON = JsonConverter.ParseJson(objHTTP.ResponseText)
'strResult = objHTTP.ResponseText
'MsgBox JSON(1)("BTC")("sellPrice")
baseCol = 9
buyCol = 10
sellCol = 11
i = 1
Dim keyCurr As String
For Each Item In JSON
ActiveSheet.Cells(i + 2, baseCol).Value = Item.Keys
i = i + 1
Next
Kinly help. As you could see in a comment above, I am able to get data as hard coded
MsgBox JSON(1)("BTC")("sellPrice")
But when I try getting that in loop, I am unable to. Below are the ones I tried but did not work.
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(Item.Keys)("sellPrice")
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(" + Item.Keys + ")("sellPrice")
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(Item(0))("sellPrice")
ActiveSheet.Cells(i + 2, baseCol).Value = JSON(i)(Item(1))("sellPrice")
For parsing JSON, I use vbaJSON library. It seem to return proper object (as could see am able to access hard coded way, but could not access in loop)
Update: As per Vityata's hint, below code seem to be working fine. Thank you all for such immediate help. :)
For Each Item In JSON
ActiveSheet.Cells(i + 2, baseCol).Value = Item.Keys
For Each curr In Item
ActiveSheet.Cells(i + 2, buyCol).Value = JSON(i)(curr)("buyPrice")
ActiveSheet.Cells(i + 2, sellCol).Value = JSON(i)(curr)("sellPrice")
i = i + 1
Next curr
Next Item
It you hard-code the "sellPrice", you can come up with something like this:
And in the immediate window:
The keys and the items are collection, which can be looped through:
In the immediate window:
Not a perfectly tidy, yet, version but here goes:
Version 2 (1 less loop) - I switched to reading the JSON from file due to API call time outs
Version 1: