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.
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,309
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
 

NArawna

New Member
Joined
Dec 5, 2018
Messages
10
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
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,309
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,070
Messages
5,484,507
Members
407,444
Latest member
Trick

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top