VBA JSON - check if a property exist

From Check if property exist · Issue #56 · VBA-tools/VBA-JSON · GitHub


Is their any way to check if a property exist? When I try to access a property that is not in a JSON file it throws a 450 error.

@timhall
Member

Hi @Martin-Hogge you can use Exists for objects and check for array bounds with Count.

Dim Value As Dictionary
Set Value = JsonConverter.ParseJson("{""a"":[1,2,3],""b"":{""c"":""d""}}")

If Value.Exists("a") Then
  For i = 1 to Value("a").Count
    '     ^ (Collections are 1-based)
  Next i
End If

If Not Value.Exists("missing") Then
  ' Not found
End If

Additionally, you can check what keys an object (Dictionary) contains with Keys.

Dim Value As Dictionary
Set Value = JsonConverter.ParseJson("{""a"":1,""b"":2,""c"":3})

Dim Key As Variant
'          ^ Variant is needed for For Each with strings

For Each Key In Value.Keys
  Debug.Print Key
Next Key
' -> a, b, c

 

Comments

Popular posts from this blog

Add GitHub Repository to DBeaver CE Secured with SSH Protocol

Keyboard Shortcut to "Toggle Line Comments" in DBeaver

DBeaver Shortcuts