I am trying to convert a json api to excel table. I tried different parsing methods but currently using VBA-JSON (similar to VB-JSON but faster parsing). So far I got it to
Have you tried calling the web service via the vba-web toolkit (from the same people who made vba-json)? It automatically wraps the JSON result into a data object.
I then created a Function that converts a simple table-like JSON into a 2D array, which I then paste it into a Range.
First, here's the function you can add to your code:
' Converts a simple JSON dictionary into an array
Function ConvertSimpleJsonToArray(data As Variant, ParamArray columnDefinitionsArray() As Variant) As Variant
Dim NumRows, NumColumns As Long
NumRows = data.Count
NumColumns = UBound(columnDefinitionsArray) - LBound(columnDefinitionsArray) + 1
Dim ResultArray() As Variant
ReDim ResultArray(0 To NumRows, 0 To (NumColumns - 1)) 'Rows need an extra header row but columns do not
Dim x, y As Integer
'Column headers
For y = LBound(columnDefinitionsArray) To UBound(columnDefinitionsArray)
ResultArray(LBound(ResultArray), y) = columnDefinitionsArray(y)
Next
'Data rows
For x = 1 To NumRows
For y = LBound(columnDefinitionsArray) To UBound(columnDefinitionsArray)
ResultArray(x, y) = data(x)(columnDefinitionsArray(y))
Next
Next
ConvertSimpleJsonToArray = ResultArray
End Function
Here's how I tried calling your API and populating just 4 columns into Excel:
Sub Auto_Open()
Dim FocusClient As New WebClient
FocusClient.BaseUrl = "https://www.gw2shinies.com/api"
' Use GetJSON helper to execute simple request and work with response
Dim Resource As String
Dim Response As WebResponse
'Create a Request and get Response
Resource = "json/item/tp"
Set Response = FocusClient.GetJson(Resource)
If Response.StatusCode = WebStatusCode.Ok Then
Dim ResultArray() As Variant
ResultArray = ConvertSimpleJsonToArray(Response.data, "item_id", "name", "type", "subtype")
Dim NumRows, NumColumns As Long
NumRows = UBound(ResultArray) - LBound(ResultArray) + 1
NumColumns = UBound(ResultArray, 2) - LBound(ResultArray, 2) + 1
ActiveSheet.Range("a1").Resize(NumRows, NumColumns).Value = ResultArray
Else
Debug.Print "Error: " & Response.Content
End If
End Sub
Yes it does take a few seconds to run, but that's more likely to the 26000 rows you have. Even loading the raw JSON in Chrome took a few seconds and this has JSON parsing and loading into array on top of it. You can benchmark the code by Debug.Print timestamps after each code block.
I hope that helps!