Outputting a JSON response to sheets.

NArawna

New Member
Joined
Dec 5, 2018
Messages
10
Hi, I am using VBA-Web to pull in some data in a json format via REST.
Which works fine using

Code:
Public Sub PullStaff()Dim Client As New WebClient
Dim Request As New WebRequest
Dim RequestResponse As WebResponse


 WebHelpers.EnableLogging = True
 
Client.BaseUrl = "https://web.site/webservice/rest/server.php"

Request.Method = WebMethod.HttpGet
Request.ResponseFormat = WebFormat.Json
Request.AddQuerystringParam "wsfunction", "core_enrol_get_enrolled_users"
Request.AddQuerystringParam "moodlewsrestformat", "json"
Request.AddQuerystringParam "courseid", "1234"
Request.AddQuerystringParam "wstoken", "111111111111111"
Request.AddHeader "Authorization", "Token ..."


Request.AddQuerystringParam "options[0][name]", "userfields"
Request.AddQuerystringParam "options[0][value]", "id"
Request.AddQuerystringParam "options[1][name]", "userfields"
Request.AddQuerystringParam "options[1][value]", "fullname"
Request.AddQuerystringParam "options[2][name]", "userfields"
Request.AddQuerystringParam "options[2][value]", "email"
Request.AddQuerystringParam "options[3][name]", "userfields"
Request.AddQuerystringParam "options[3][value]", "username"


Set RequestResponse = Client.Execute(Request)
'Debug.Print RequestResponse.Content
End Sub

I get back a response which I can see in the immediate window such as

Code:
<-- Response - 10:30:22
200 OK
Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0
Connection: close
Date: Wed, 05 Dec 2018 10:30:22 GMT
Pragma: no-cache
Content-Length: 874
Content-Type: application/json
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Accept-Ranges: none
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/7.1.11
Access-Control-Allow-Origin: *


[{"id":1234,"username":"abc","fullname":"B Smoke","email":"smoke@website.com"},{"id":1235,"username":"def","fullname":"D Blain","email":"blain@website.com"},

I am now trying to get this into a sheet so I can do some work on it but I have hit a brick wall with this.
I tried

Code:
Dim i As Integer
i = 4
For Each item In RequestResponse
    Sheets(1).Cells(i, 1).Value = item("id")
    Sheets(1).Cells(i, 2).Value = item("fullname")
    Sheets(1).Cells(i, 3).Value = item("email")
    Sheets(1).Cells(i, 4).Value = item("username")
    i = i + 1
Next

Based off another forum thread I found but its throwing the error "Variable not defined" on
Code:
For Each item In RequestResponse

I am not sure what I should define item as to get this working, or if this even will work? am I totally off base with what I am trying to achieve?

Thanks for any advice on getting this working.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You seem to be using https://github.com/VBA-tools/VBA-Web. I've never used VBA-Web, but looking at the GetJSON example it looks like you should be using RequestResponse.Data, which is the data structure consisting of Dictionaries and Collections of the parsed json string. Therefore try this:
Code:
Dim i As Integer, item As Object
i = 4
For Each item In RequestResponse.Data
    Sheets(1).Cells(i, 1).Value = item("id")
    Sheets(1).Cells(i, 2).Value = item("fullname")
    Sheets(1).Cells(i, 3).Value = item("email")
    Sheets(1).Cells(i, 4).Value = item("username")
    i = i + 1
Next
 
Upvote 0
You seem to be using https://github.com/VBA-tools/VBA-Web. I've never used VBA-Web, but looking at the GetJSON example it looks like you should be using RequestResponse.Data, which is the data structure consisting of Dictionaries and Collections of the parsed json string. Therefore try this:
Code:
Dim i As Integer, item As Object
i = 4
For Each item In RequestResponse.Data
    Sheets(1).Cells(i, 1).Value = item("id")
    Sheets(1).Cells(i, 2).Value = item("fullname")
    Sheets(1).Cells(i, 3).Value = item("email")
    Sheets(1).Cells(i, 4).Value = item("username")
    i = i + 1
Next

Thanks I got it working, just to confirm what is the benefit of putting i = i + 1 at the end rather than the beginning of the loop?
This is the code I got working from yesterday
Code:
For Each item In RequestResponse.Data
    i = i + 1 ' i is an integer here
    Sheets(3).Cells(i, 1).Value = item("id")
    Sheets(3).Cells(i, 2).Value = item("fullname")
    Sheets(3).Cells(i, 3).Value = item("email")
    Sheets(3).Cells(i, 4).Value = item("username")


Next
 
Upvote 0
There is no benefit or disadvantage. Where you put i = i + 1 depends on its initial value before the loop and which row number the data is written to first. Incrementing i at the start of the loop means the first row is 5, whereas incrementing it at the end means the first row is 4.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top