0

Scenario: I am using some functions to retrieve data from a website with VBA. These functions currently read the data into a JSON file and with some more processing paste it into a worksheet.

Issue: Having to clear and write to sheets every time a new JSON is retrieved hinders the performance of the code.

Question: Would it be possible to parse the data inside the JSON object directly into a Variant Array, so that any operations with it are done in memory?

What I already tried: following up from some research online I found Parsing Json array via VBA and http://excelerator.solutions/2017/08/16/import-json-to-excel-using-vba/ and some other pages. In all these cases, the data is parsed first into a table or directly into a worksheet, but not into an array.I tried modifying my function that writes the JSON to a sheet, by parsing through the table but that would still require me to have the "paste to worksheet" step, before reading it into an array.

Function I am working with: When I call this function, my JSON file is already retrieved and stored in memory. Also, the way this works is by iteratively parsing through each offset, which would be equivalent to the rows of the array. My idea was to skip this altogether as parse the jSON directly.:

Public Sub WriteJsonToSheet(s As Worksheet, j As JSON, Optional ByVal fields As Variant, Optional rowOffset As Long = 0)
    If (rowOffset = 0) Then
    Dim loopvar1 As Long, loopvar2 As Long
        With s.UsedRange
            .ClearFormats
            .ClearContents
            .ClearComments
            .ClearHyperlinks
            .ClearOutline
            .ClearNotes
        End With                                 's.UsedRange.Clear
    End If
    If j.count = 0 Then Exit Sub

    ' get the list of all the fields in the -first- record
    If IsMissing(fields) Then
        fields = j.item("0.$").GetKeys("$.")
    End If

    ' create a table and write to the spreadsheet
    If (rowOffset = 0) Then
        WriteRowToRange s.Cells(1, 1), fields
        s.rows(1).AutoFilter
        s.rows(1).Font.Bold = True
    End If

    ' this is part of the original function, to paste to a worksheet
    If (j.count > 0) Then
        'Dim table As Variant
        table = JsonArrayToTable(j, fields)
        WriteTableToRange s.Cells(2 + rowOffset, 1), table
    End If
End Sub

I created this to add to my code, but the code just skips this part and no action takes place:

    ' This is my attempt to past it directly to an array
        Dim table As Variant
        table = JsonArrayToTable(j)
        For loopvar1 = 0 To UBound(table, 2)
            'For loopvar2 = 1 To UBound(table, 2)
                TEST.Cells(1, loopvar1 + 1) = table(0, loopvar1)
            'Next loopvar2
        Next loopvar1

OBS. The data in the JSON can differ greatly, so the idea was to create a general function that could pass a JSON into an array. In this case, the example of data would be (inside my code, this JSON is retrieved for multiple IDs. Each time one is retrieved, it is pasted to a new row of the output worksheet):

{
"Response": {
        "dm": [
                {
                  "$": {  
                       "id": ""830778192",
                       "end_period": ""2023-10-25T00:00:00",
                       "end_period1": "2021-01-25T00:00:00",
                       }
                }
              ]
            }
}
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
DGMS89
  • 1,507
  • 6
  • 29
  • 60
  • Would be helpful to know how your JSON object is structured. – Tim Williams Mar 11 '20 at 16:41
  • @TimWilliams The data varies, that is why my idea was just creating a function capable of adding a JSON into an array. But usually is structured like an example I added to the OP. – DGMS89 Mar 11 '20 at 16:46
  • What about this? https://dymeng.com/parsing-json-with-vba/ – Marc Mar 11 '20 at 16:51
  • You might be able to work on it in Power Query, depending on the manipulations you need to do. – Ron Rosenfeld Mar 11 '20 at 16:52
  • Seems like a general function would be difficult for arbitrarily-nested JSON structures. It's not so easy to "flatten" JSON objects to a 2D array. Defintely can be done for any known structure where you have already determined how it should be represented as an array. – Tim Williams Mar 11 '20 at 16:55
  • https://stackoverflow.com/questions/60604661/excel-vba-get-data-from-string-in-rows#comment107220100_60604661 – Naresh Mar 11 '20 at 17:02

0 Answers0