Potentially been asked before, however I’m after a quick solution to a specific problem with these nested arrays! I have the following data being pulled using Excel into a text file from a url (edited due to confidential info) The data is as follows:
{
"ret": {
"getDetailsOutput": {
"searchTime": "1717399860000",
"startTime": "1717316816470",
"endTime": "1717403216470",
"cptList": null,
"routeDispatchDetailMap": {
"Lane1": {
"allSfs": [],
},
"Lane2": {
"allSfs": [],
},
"Lane3": {
"allSfs": [],
},
"Lane4": {
"allSfs": [],
},
"Lane5": {
"allSfs": [
"Nested1",
"Nested2",
"Nested3",
"Nested4"
],
},
"Lane6": {
"allSfs": [
"Nested1",
"Nested2"
],
},
"Lane7": {
"allSfs": [
"Nested1",
"Nested2",
"Nested3",
"Nested4",
"Nested5",
"Nested6"
],
},
}
}
},
"ok": true,
"message": "success"
}
I am currently toying with following VBA code to access the Nested values in and the Lane arrays, under the allSfs section of each of the lanes. Sometimes these have multiple values, sometimes they are empty:
Set J = ParseJson(H.responseText)
Set J = J("ret")("getDetailsOutput")("routeDispatchDetailMap")
write_to_text H.responseText
WS.Range("A1:G200").Clear
i = 0
On Error Resume Next
For Each Data In J
WS.[A2].Offset(i) = Data
Set X = WS.[A2].Offset(i)
For Each X In J("ret")("getDetailsOutput")("routeDispatchDetailMap")(WS.[A2].Offset(i).Value)(0)
Debug.Print WS.[A2].Offset(i).Value
WS.[A2].Offset(i) = X
i = i + 1
Next X
i = i + 1
Next Data
My end game goal is to have the list of Nested Values in column A and then the respective Lane name in column B…. as later on in my plans I will need to use a VLOOKUP formula to access the Lane, using the specific Nested value, if that makes any sense? I’ve tried quite a lot of things but I just can’t seem to access the Nested values… Any advice? Thanks in advance
2
Answers
When I deal with complex nested structures, I usually split the complexity by defining variables for every level – this makes it much easier to read and debug (and no, this doesn’t slow down the code).
I ended up with something like this:
Using the debugger and the locals window (View->Locals), it shows step by step the content of the JSon. It reveals for example that "arrays" are stored in Collections.
Note: I assume that you use the JSonConverter (https://github.com/VBA-tools/VBA-JSON). You should mention that in your question. My version didn’t like the commas that you have behind the array
"allSfs": [],
and I had to remove them manually.