API calls from Excel, transform JSON API response to worksheet

Costas

Administrator
Staff member
Code:
'add reference to
'Microsoft XML, v6.0
'Microsoft Scripting Runtime

Sub getUsers()
    Dim req As MSXML2.ServerXMLHTTP60
    Dim Parsed As Collection
    Dim i As Integer
    
    Set req = New MSXML2.ServerXMLHTTP60

    api_url = "https://jsonplaceholder.typicode.com/users"
    
    req.Open "GET", api_url, False
    
    'If credentials are required use:
    'req.setRequestHeader "Authorization", "token 7a7b519704cc560:41aa8696c1d2a5d"
    
    req.send
    
    Set Parsed = JsonConverter.ParseJson(req.responseText)
    
    'Setting the header
    Worksheets("Sheet1").Cells(1, "A").Value = "id"
    Worksheets("Sheet1").Cells(1, "B").Value = "name"

    Dim Value As Dictionary
    i = 2

    For Each Value In Parsed
        Worksheets("Sheet1").Cells(i, "A").Value = Value("id")
        Worksheets("Sheet1").Cells(i, "B").Value = Value("name")
        i = i + 1
    Next
        
End Sub

src - https://acmsoftware.cl/api-calls-from-excel
 
Top