You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, im trying to parse a responsetext from an http error 422, this is the string value response req.responsetext:
{"error":{"message":"Il totale dei pagamenti non corrisponde al totale da pagare.","validation_result":null}}
dim parsed ad dictionary
set parsed = jsonconverter.jsonparse(req.responsetext) and my json and debug:
Dim key As Variant
For Each key In Parsed.Keys
Debug.Print key, Parsed(key)
Next key
result error runtime 450 "Wrong number of arguments" because parsed have only "error" key but "nothing" as value and not second level.. please help me and sorry for my english...
The issue with your code is that you don't understand what you are getting back from ParseJson. Basically you are being returned a nested set of objects. The variable parsed is a Scripting.Dictionary object that has one key named error. The value for the key named error is another Scripting.Dictionary object that has two keys named message and validation_result, where the key named message has a string value and the key named validation_result has the null value. Now that you understanding what is being returned from ParseJson there is a disconnect with the code that you have written which is why you are receiving the error you mentioned. I believe what you are trying to accomplish is to iterate the keys of the Scripting.Dictionary value of the error key. The following code accomplishes this by first getting the Json document Scripting.Dictionary object, then getting the error key's Scripting.Dictionary object, then iterating over the keys to produce the desired output in the Immediate pane:
PublicSubissue266()
Const json AsString = "{""error"":{""message"":""Il totale dei pagamenti non corrisponde al totale da pagare."",""validation_result"":null}}"Dim parsed AsScripting.Dictionary
Dim error AsScripting.Dictionary
Dim key AsVariantDim item AsVariantSet parsed = JsonConverter.ParseJson(json)
For Each key In parsed.keys
If key = "error"ThenSet error = parsed.item(key)
For Each item In error.keys
Debug.Print item & " = " & VBA.IIf(VBA.IsNull(error.item(item)), "null", error.item(item))
Next item
End IfNext key
Exit SubEnd Sub
which yields the following in the Immediate pane of the VBA project window:
message = Il totale dei pagamenti non corrisponde al totale da pagare.
validation_result = null
If you just want the response in a more readable form for logging, convert it back with 'pretty print': Debug.Print Now; "Error"; req..Status; vbCr; ConvertToJson(Parsed, 2)
The value of the 'error' key pair is not a printable string; it's a reference to a Dictionary. To get the error message so that it can be reported to a higher level, you have to follow the links.
Something like this:
Dim errDict As Dictionary, errSts As Long, errtext As String
' errSts = req.Status
errSts = 422
Select Case errSts
Case 200
' ...
Case 422 ' 422 Unprocessable Content
Set errDict = Parsed("error")
errtext = errDict("message")
' add text for validation result if helpful
Err.Raise 10000 + errSts, , errtext
Case Else
' ...
End Select
If the above solves your problem, please close the issue here.
Activity
houghtonap commentedon Feb 26, 2024
The issue with your code is that you don't understand what you are getting back from ParseJson. Basically you are being returned a nested set of objects. The variable
parsedis a Scripting.Dictionary object that has one key namederror. The value for the key namederroris another Scripting.Dictionary object that has two keys namedmessageandvalidation_result, where the key namedmessagehas a string value and the key namedvalidation_resulthas the null value. Now that you understanding what is being returned from ParseJson there is a disconnect with the code that you have written which is why you are receiving the error you mentioned. I believe what you are trying to accomplish is to iterate the keys of the Scripting.Dictionary value of theerrorkey. The following code accomplishes this by first getting the Json document Scripting.Dictionary object, then getting theerrorkey's Scripting.Dictionary object, then iterating over the keys to produce the desired output in theImmediatepane:which yields the following in the
Immediatepane of the VBA project window:Nick-vanGemeren commentedon Feb 26, 2024
If you just want the response in a more readable form for logging, convert it back with 'pretty print':
Debug.Print Now; "Error"; req..Status; vbCr; ConvertToJson(Parsed, 2)The value of the 'error' key pair is not a printable string; it's a reference to a Dictionary. To get the error message so that it can be reported to a higher level, you have to follow the links.
Something like this:
If the above solves your problem, please close the issue here.