I am using Excel VBA together with the excellent https://github.com/VBA-tools/VBA-JSON parser which converts a json string to a Dictionary to parse API responses from UPS.
I am getting error messages when parsing the json responses because the responses I get are non-consistent.
For example, a response may look like this:
{"Fault":{"faultcode":"Client", "faultstring":"An exception has been raised as a result of client data.", "detail":{"Errors":{"ErrorDetail":{"Severity":"Hard", "PrimaryErrorCode":{"Code":"9150002", "Description":"Invalid or missing inquiry number - TrackingNumber, ShipmentIdentificationNumber, or ReferenceNumber"}}}}}}
But it can also be:
{"TrackResponse":{"Response":{"ResponseStatus":{"Code":"1", "Description":"Success"}, "TransactionReference":""}, "Shipment":{"InquiryNumber":{"Code":"01", "Description":"ShipmentIdentificationNumber", "Value":"1Z1291249124949"}, "ShipperNumber":"55443322", "ShipmentAddress":[{"Type":{"Code":"01", "Description":"Shipper Address"}, "Address":{"AddressLine":"16 test", "City":"test city", "PostalCode":"23445", "CountryCode":"US"}}, {"Type":{"Code":"02", "Description":"ShipTo Address"}, "Address":{"City":"TEST CITY", "PostalCode":"12345", "CountryCode":"US"}}], "ShipmentWeight":{"UnitOfMeasurement":{"Code":"KGS"}, "Weight":"38.00"}, "Service":{"Code":"011", "Description":"UPS Standard"}, "ReferenceNumber":[{"Code":"13", "Value":"923484MBS"}, {"Code":"16", "Value":"42111"}], "PickupDate":"20210118", "DeliveryDetail":{"Type":{"Code":"03", "Description":"Scheduled Delivery"}, "Date":"20210120", "Time":"235900"}, "Package":[{"TrackingNumber":"1Z609R234234234234", "Activity":{"ActivityLocation":{"Address":{"CountryCode":"US"}}, "Status":{"Type":"M", "Description":"Shipper created a label, UPS has not received the package yet.", "Code":"MP"}, "Date":"20210118", "Time":"104442"}, "PackageWeight":{"UnitOfMeasurement":{"Code":"KGS"}, "Weight":"19.00"}, "ReferenceNumber":{"Code":"19", "Value":"42111"}}, {"TrackingNumber":"1Z23535235235", "Activity":{"ActivityLocation":{"Address":{"CountryCode":"US"}}, "Status":{"Type":"M", "Description":"Shipper created a label, UPS has not received the package yet.", "Code":"MP"}, "Date":"20210118", "Time":"104442"}, "PackageWeight":{"UnitOfMeasurement":{"Code":"KGS"}, "Weight":"19.00"}, "ReferenceNumber":{"Code":"19", "Value":"42111"}}]}}}
So when I try to do:
If json("TrackResponse")("Response")("ResponseStatus")("Description") = "Success" Then
Debug.Print "The response is Success"
End if
and the response was like the first example I get the following error message:
Run-time error 13:
Type mismatch
I do not know how to solve this, as I off course cannot predict what the response will be beforehand.
Any help would be greatly appreciated.