Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue parsing text to json #266

Open
giacgiac opened this issue Feb 26, 2024 · 2 comments
Open

Issue parsing text to json #266

giacgiac opened this issue Feb 26, 2024 · 2 comments

Comments

@giacgiac
Copy link

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...

@houghtonap
Copy link

houghtonap commented 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 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:

Public Sub issue266()

  Const json As String = "{""error"":{""message"":""Il totale dei pagamenti non corrisponde al totale da pagare."",""validation_result"":null}}"
  
  Dim parsed As Scripting.Dictionary
  Dim error As Scripting.Dictionary
  
  Dim key As Variant
  Dim item As Variant
  
  Set parsed = JsonConverter.ParseJson(json)
  
  For Each key In parsed.keys
    If key = "error" Then
      Set 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 If
  Next key
  
  Exit Sub
End 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

@Nick-vanGemeren
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants