VBA JSON Parsing - key/value doesn't exist (1 Viewer)

peskywinnets

Registered User.
Local time
Today, 20:03
Joined
Feb 4, 2014
Messages
576
I'm trying to parse using the popular VBA JSON parser, but because a key doesn't exist, my code breaks with a Run-Time error 13 Type Mismatch.

Here's my line of code...

EbayTaxType = json("lineItems")(1)("ebayCollectAndRemitTaxes")(1)("taxType")

...I've done a google search, but I'm going around in circles, would anyone be able to offer up a solution to check whether.. ..

"lineItems")(1)("ebayCollectAndRemitTaxes")(1)("taxType"

...exists as a boolean?

(because then I can work around things)

Many thanks.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:03
Joined
Oct 29, 2018
Messages
21,590
I suppose one way is to just catch the error.
Code:
On Error Resume Next
EbayTaxType = json("lineItems")(1)("ebayCollectAndRemitTaxes")(1)("taxType")
If Err <> 0 Then
    EbayTaxType = "Missing"
End If
On Error GoTo 0
Just a thought...
 

peskywinnets

Registered User.
Local time
Today, 20:03
Joined
Feb 4, 2014
Messages
576
I appreciate the input ...I may be mis-intpreting how to use what you've suggested - the error is thrown up in the json module itself (not in the function where my code is) ....& I don't want to deploy anything in the JSON module

I'll google a bit more, because I find it difficult to believe that whoever came up with the (great) JSON parser for VBA hasn't got a way of checking if a path/value exists :)
 

561414

Active member
Local time
Today, 14:03
Joined
May 28, 2021
Messages
279
If you are parsing a JSON string, regardless of where it comes, that module is probably going to convert the JSON into a dictionary object. You only posted here how you are trying to handle it, but we do not know how the object looks, so we can not help much. The value of your key taxType is probably a string because it comes from the JSON, but you can not know until you actually look at it. One thing you can do is post your JSON object here to check it out. Another thing you can do is declare EbayTaxType as Variant.
 

Users who are viewing this thread

Top Bottom