Skip to content

Issue parsing text to json #266

Open

Description

@giacgiac

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

Activity

  1. houghtonap commented on Feb 26, 2024

    @houghtonap

    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
  2. Nick-vanGemeren commented on Feb 26, 2024

    @Nick-vanGemeren

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions