I am working to get my code to loop through arrays of JSON strings (of same format) until it reaches the end of the arrays (i.e., no strings left). I need the code to recognize it has reached the end by identifying that a certain identifier (present in every set) does not have additional information in the next array. So I believe I am looking for "while" syntax that says "while this identifier has content" proceed parsing the JSON according to the below code. My existing code works for array of strings for which I know the length - unfortunately the lengths are variable therefore I need flexible syntax to adjust with the lengths (i.e., "For 0 to n" doesn't work every time).
The JSON code I am parsing is in this format:
Here the identifier structure I'd like to augment with some type of while loop (the "i" is the index number depending on the # of loop in the yet to be implemented "while" loop).
So ideally looking for something like:
Please note again, everything works when I know the length -- just looking for a small syntax update, thank you! Full code below:
The JSON code I am parsing is in this format:
Code:
{"id":1,"prices":[{"name":"expressTaxi","cost":{"base":"USD4.50"....}}Code:
Json("prices")(i)("name")Code:
"While Json("prices")(i)("name") has information" then proceed on....Code:
Option Explicit
Sub getJSON()
sheetCount = 1
i = 1
urlArray = Array("URL1", “URL2”, “URL3”)
Dim MyRequest As Object: Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim MyUrls: MyUrls = urlArray
Dim k As Long
Dim Json As Object
For k = LBound(MyUrls) To UBound(MyUrls)
With MyRequest
.Open "GET", MyUrls(k)
.Send
Set Json = JsonConverter.ParseJson(.ResponseText)
''[where I’d like some type of While statement checking into the below line for content]
Sheets("Sheet" & sheetCount).Cells(i, 1) = Json("prices")(i)("name")
Sheets("Sheet" & sheetCount).Cells(i, 2) = Json("prices")(i)("cost")("base")
i = i + 1
End With
sheetCount = sheetCount + 1
Next
End Sub