Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access a collection in VBA-JSON? #252

Open
wanderleihuttel opened this issue May 30, 2023 · 1 comment
Open

How to access a collection in VBA-JSON? #252

wanderleihuttel opened this issue May 30, 2023 · 1 comment

Comments

@wanderleihuttel
Copy link

Hello Guys

How can I get data from Json "Name" and "Options"?
How Option keys are always different, I would like to access using maybe the index?

I've read some another issues, but I didn't understand very well.

It would be interesting to include more examples in github, this could clarify how to use and avoid new issues.

My Json

{
    "Name": "Report001",
    "Fields": [
        { "Name": "pCompetInicial" },
        { "Name": "pCompetFin" },
        { "Name": "pGrupoProcessamento" },
        { "Name": "pCodigoEmpresa" },
        { "Name": "pQuebra", 
          "Options": [
                { "Option XPTO": 0 },
                { "Option X": 1 },
                { "Option FOO": 2 },
                { "Option BAR": 3 }
        ]},
        { "Name": "pOrdenar",
          "Options": [
                { "Some Informarmation": 0 },
                { "Another Informarmation": 1 },
                { "Test": 2 }
        ]},
        { "Name": "pCodigoIdioma" }
    ],
    "Version": "23.5.0.0"
}
Sub TestJson()
    Dim JSON As Scripting.Dictionary
    Dim vItem As Scripting.Dictionary
    Dim vResponse As String
    Dim vArray As VBA.Collection
    Dim vDict As Scripting.Dictionary
    Dim vChild As Scripting.Dictionary
    
    'Response from Http Request
    vResponse = "{'Name':'Report001','Fields':[{'Name':'pCompetInicial'},{'Name':'pCompetFin'},{'Name':'pGrupoProcessamento'},{'Name':'pCodigoEmpresa'},{'Name':'pQuebra','Options':[{'Option XPTO':0},{'Option X':1},{'Option FOO':2},{'Option BAR':3}]},{'Name':'pOrdenar','Options':[{'Some Informarmation':0},{'Another Informarmation':1},{'Test':2}]},{'Name':'pCodigoIdioma'}],'Version':'23.5.0.0'}"
    vResponse = Replace(vResponse, "'", """")
    
    Set JSON = JsonConverter.ParseJson(vResponse)
    
    'Access single node
    Debug.Print JSON("Version")
    
    'Access a node with children
    For Each vItem In JSON("Fields")
        If vItem.Exists("Options") Then
        
            If TypeOf vItem("Options") Is VBA.Collection Then
                'How do I Access "Options" (all indexes and values) (by Key or Value or index)
            End If
        
            Debug.Print vItem("Name") & " - Exist Options"
            
        Else
            Debug.Print vItem("Name")
        End If
    Next vItem

End Sub

Thanks

@Nick-vanGemeren
Copy link

You process Options in the same way that you process Fields.
Each option is (should be) a Dictionary. You get the list of keys with the Keys method.

Change the loop contents to ...

        If vItem.Exists("Options") Then
            Debug.Print vItem("Name") & " - Exist Options"
            ProcessOptions vItem("Options")
        Else
            Debug.Print vItem("Name")
        End If

and then add ...

Sub ProcessOptions(vOptions As Variant)
    Dim vDict As Scripting.Dictionary
    Dim vKey As Variant
    If Not TypeOf vOptions Is VBA.Collection Then
        Debug.Print "*** Options not a collection = "; TypeName(vOptions)
        Exit Sub
    End If
    For Each vDict In vOptions
        For Each vKey In vDict.Keys
            Debug.Print "", vKey, vDict(vKey)
        Next vKey
    Next vDict
End Sub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants