VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.74k stars 565 forks source link

Invalid procedure call or argument #261

Closed generic-git-user closed 8 months ago

generic-git-user commented 8 months ago

I saw one other post on this issue but the solution hasn't worked for me. I am able to successfully pull the json data and print it but when I attempt to access one of the specific keys I get this error. I'm on a mac using the dictionary.cls file also.

Screen Shot 2023-12-17 at 12 03 54 PM
houghtonap commented 8 months ago

The issue appears to be your understanding of how VBA-JSON maps Json into VBA objects. VBA-JSON uses two objects that have different interfaces for accessing the corresponding Json data. A Json Array is mapped to a VBA Collection object and a Json Object is mapped to a Scripting.Dictionary custom object. You need to read and understand these objects and how to interface with them by reading the documentation on Microsoft's website.

Therefore, the Json you retrieved was (abstracted):

[ { }, { } ]

Which represents a VBA collection object which contains n-items of object references to Scripting.Dictionary custom objects.

Therefore, when your code tries to access these nested object references it indicates:

Debug.Print json("id")

Which is incorrect based on nested object structure that VBA-JSON creates. What you have not accounted for is the VBA Collection object that was created, and you used the incorrect interface for accessing the Scripting.Dictionary custom object.

First you need to decide which Json Object in the Json Array you want to access. Let's assume the first element which is at by position zero. To get that Json Object we need to:

Set first = json(0)

Next you appear to want to access the id property of the Json Object and to do that we need to:

If first.Exists("id") _ Then Debug.print "Property id is " & first.Item("id") Else Debug.Print "Property id does not exist" End If

Because of how the Scripting.Dictionary custom object works you must check for existence of the key before access, otherwise it will automatically create the key.

Hope that helps, Andrew


From: generic-git-user @.> Sent: Sunday, December 17, 2023 2:07:22 PM To: VBA-tools/VBA-JSON @.> Cc: Subscribed @.***> Subject: [VBA-tools/VBA-JSON] Invalid procedure call or argument (Issue #261)

I saw one other post on this issue but the solution hasn't worked for me. I am able to successfully pull the json data and print it but when I attempt to access one of the specific keys I get this error. I'm on a mac using the dictionary.cls file also.

Screen.Shot.2023-12-17.at.12.03.54.PM.png (view on web)https://github.com/VBA-tools/VBA-JSON/assets/34421371/2b31b582-5974-409a-84bf-201d1d574e1d

— Reply to this email directly, view it on GitHubhttps://github.com/VBA-tools/VBA-JSON/issues/261, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIKTRAZT5JXTIFTQM4VLS4LYJ47GVAVCNFSM6AAAAABAYRTUTSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA2DKMZTHAZTSMQ. You are receiving this because you are subscribed to this thread.Message ID: @.***>

generic-git-user commented 8 months ago

Correct on all points! :) Thanks for the quick response.

Nick-vanGemeren commented 8 months ago

Your screenshot does not show which statement is causing the error (the yellow highlight when you press Debug). I'm guessing it's the Debug.Print json("id").

If the data starts as shown in the Immediate window, thenjsonconsists of a VBA Collection (JSON Array) of VBA Dictionaries (JSON Object).

So your debugging print should probably look like

    Debug.Print "Found"; json.Count ; "records"
    For Each dict in json
        Debug.Print dict.Item("id")
    Next

Note 1: Your function returnssResult, notjson. I assume that in production, parsing will be done later after error checking.

Note 2: Collection items can be accessed by key, provided that the key was specified when the item was added. But VBA-JSON does not do this. The error 5 in this case effectively means 'key not found'.

Andrew:

=================== If this solves your problem, please close the issue here.