shahin
Active Member
I'm trying to fetch two fields from each container from some json response using regex. When I execute the script that I've written so far can produce the two fields from all the containers. However, the way I've defined the last loop doesn't seem to be an ideal one. To be clearer, I used the count of name and created a loop to parse the required fields. If the count of `names` and `changeAmount` are different the results will be real messy. How can I rectify the loop to scrape the two fields in the right way?
I've tried with (working one):
The following are the type of containers I'm scraping those two fields from:
PS I would like to stick to the way I've already tried and I'm not after any solution related to any json converter.
I've posted the same problem in here https://stackoverflow.com/questions...l-the-containers-out-of-some-json-response-in as well.
I've tried with (working one):
Code:
Sub FetchContent()
Const Url$ = "https://api-global.morningstar.com/sal-service/v1/stock/ownership/v1/0P000000GY/OwnershipData/mutualfund/20/data?locale=en&clientId=MDC&benchmarkId=category&version=3.21.1"
Dim elem As Object, oelem As Object, I&, R&, S$
Dim Http As Object, Rgxp As Object, wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Set Http = CreateObject("MSXML2.XMLHTTP")
Set Rgxp = CreateObject("VBScript.RegExp")
With Http
.Open "GET", Url, False
.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
.setRequestHeader "ApiKey", "lstzFDEOhfFNMLikKa0am9mgEKLBl49T"
.send
S = .responseText
End With
With Rgxp
.Global = True
.MultiLine = True
.Pattern = "name"":""(.*?)"""
Set elem = .Execute(S)
.Pattern = "changeAmount"":(.*?),"
Set oelem = .Execute(S)
End With
For I = 0 To elem.Count - 1
R = R + 1: ws.Cells(R, 1) = elem(I).SubMatches(0)
ws.Cells(R, 2) = oelem(I).SubMatches(0)
Next I
End Sub
The following are the type of containers I'm scraping those two fields from:
Code:
0: {secId: "FOUSA00FQU", name: "Vanguard Total Stock Mkt Idx Inv", totalSharesHeld: 2.564925871507663,…}
changeAmount: -1331374
changePercentage: -1.1355487246359206
currentShares: 115913617
date: "2020-04-30T00:00:00.000"
name: "Vanguard Total Stock Mkt Idx Inv"
secId: "FOUSA00FQU"
starRating: "4"
totalAssets: 4.16033
totalSharesHeld: 2.564925871507663
trend: "_PO_"
1: {secId: "FOUSA00FS1", name: "Vanguard 500 Index Investor", totalSharesHeld: 1.8912105957275436,…}
changeAmount: -487891
changePercentage: -0.5676114490562759
currentShares: 85467211
date: "2020-04-30T00:00:00.000"
name: "Vanguard 500 Index Investor"
secId: "FOUSA00FS1"
starRating: "4"
totalAssets: 5.08629
totalSharesHeld: 1.8912105957275436
trend: "_PO_"
PS I would like to stick to the way I've already tried and I'm not after any solution related to any json converter.
I've posted the same problem in here https://stackoverflow.com/questions...l-the-containers-out-of-some-json-response-in as well.
Last edited: