Results 1 to 3 of 3

Thread: Reading a JSON and looping

  1. #1

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    224

    Reading a JSON and looping

    Hello guys,
    I'm getting from the server a JSON string with the statuses of a particular actions. In this case it returns results for 2 actions.
    For
    ID: 551720
    and
    ID: 551721

    String looks like this:

    HTML Code:
    [{"ElectronicId":551720,"DocumentNr":"130/10/15","DocumentTypeId":1,"DocumentTypeName":"eInvoice","StatusId":30,"StatusName":"Sent","RecipientBusinessNumber":"0050960000","RecipientBusinessUnit":"","RecipientBusinessName":"Comp d.o.o.","Created":"2019-07-23T21:21:23.743","Updated":"2019-07-23T21:21:24.587","Sent":"2019-07-23T21:21:24.587","Delivered":null},{"ElectronicId":551721,"DocumentNr":"130/10/15","DocumentTypeId":1,"DocumentTypeName":"eInvoice","StatusId":30,"StatusName":"Sent","RecipientBusinessNumber":"00509605454","RecipientBusinessUnit":"","RecipientBusinessName":"Comp d.o.o.","Created":"2019-07-23T21:23:05.887","Updated":"2019-07-23T21:23:07.043","Sent":"2019-07-23T21:23:07.043","Delivered":null}]
    Sometimes it returns 1, sometimes 2, or maybe 20 statuses (different "ElectronicId")

    How could I loop within JSON.
    I have a code that works when I have only 1 response, but it doesn't work when I have more than 1.
    Here is the code for 1 response:

    Code:
    Dim cJS As New clsJasonParser
    
     cJS.InitScriptEngine
    
    results = """""here goes the JSON string""""""
    
     Set JsonObject = cJS.DecodeJsonString(CStr(result))
    
    
            Debug.Print cJS.GetProperty(JsonObject, "ElectronicId")
            Debug.Print cJS.GetProperty(JsonObject, "DocumentNr")
            Debug.Print cJS.GetProperty(JsonObject, "DocumentTypeId")
            Debug.Print cJS.GetProperty(JsonObject, "DocumentTypeName")
            Debug.Print cJS.GetProperty(JsonObject, "StatusId")

    Here is the code for the clsJasonParser bClass:

    Code:
    Option Explicit
    
    Private ScriptEngine As ScriptControl
    
    Public Sub InitScriptEngine()
    
        Set ScriptEngine = New ScriptControl
        ScriptEngine.Language = "JScript"
        ScriptEngine.AddCode "function getProperty(jsonObj, propertyName) { return jsonObj[propertyName]; } "
        ScriptEngine.AddCode "function getKeys(jsonObj) { var keys = new Array(); for (var i in jsonObj) { keys.push(i); } return keys; } "
    
    End Sub
    
    Public Function DecodeJsonString(ByVal JsonString As String)
    
        Set DecodeJsonString = ScriptEngine.Eval("(" + JsonString + ")")
    
    End Function
    
    Public Function GetProperty(ByVal JsonObject As Object, ByVal propertyName As String) As Variant
    
        GetProperty = ScriptEngine.Run("getProperty", JsonObject, propertyName)
    
    End Function
    
    Public Function GetObjectProperty(ByVal JsonObject As Object, ByVal propertyName As String) As Object
    
        Set GetObjectProperty = ScriptEngine.Run("getProperty", JsonObject, propertyName)
    
    End Function
    My projects:
    Virtual Forms
    VBA Telemetry

  2. #2

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    224

    Re: Reading a JSON and looping

    I'm a little bit closer to the solution, but I have another challenge.

    This is the code that I now have:

    Code:
    cJS.InitScriptEngine
    Set JsonObject = cJS.DecodeJsonString(CStr(result))
    Set JsonObject = cJS.GetObjectProperty(JsonObject, "ROOT")
    Keys = cJS.GetKeys(JsonObject)  ' svi kljuèevi iz Json stringa
        
    For lRow = 1 To UBound(Keys)
        Set Jitem = cJS.GetObjectProperty(JsonObject, Keys(lRow - 1))
        Debug.Print cJS.GetProperty(Jitem, "ElectronicId")
        Debug.Print cJS.GetProperty(Jitem, "DocumentNr")
        Debug.Print cJS.GetProperty(Jitem, "DocumentTypeId")
    
    Next lRow
    But because the JSON doesn't have the "key" only the ROOT I get error here:

    Code:
    Set JsonObject = cJS.GetObjectProperty(JsonObject, "ROOT")
    Once again here is the JSON string:

    Code:
    [{"ElectronicId":551720,"DocumentNr":"130/10/15","DocumentTypeId":1,"DocumentTypeName":"eInvoice","StatusId":30,"StatusName":"Sent","RecipientBusinessNumber":"0050960000","RecipientBusinessUnit":"","RecipientBusinessName":"Comp d.o.o.","Created":"2019-07-23T21:21:23.743","Updated":"2019-07-23T21:21:24.587","Sent":"2019-07-23T21:21:24.587","Delivered":null},{"ElectronicId":551721,"DocumentNr":"130/10/15","DocumentTypeId":1,"DocumentTypeName":"eInvoice","StatusId":30,"StatusName":"Sent","RecipientBusinessNumber":"00509605454","RecipientBusinessUnit":"","RecipientBusinessName":"Comp d.o.o.","Created":"2019-07-23T21:23:05.887","Updated":"2019-07-23T21:23:07.043","Sent":"2019-07-23T21:23:07.043","Delivered":null}]
    Does anyone have any ideas?
    My projects:
    Virtual Forms
    VBA Telemetry

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,167

    Re: Reading a JSON and looping

    Quote Originally Posted by Davor Geci View Post
    Does anyone have any ideas?
    Just use cJS.GetObjectProperty(JsonObject, 0) and cJS.GetObjectProperty(JsonObject, 1) to access first and second element of the array.

    Edit: Yes, I tried it. Although you had errors in the code above, I had to escape the JSON to put it in the "here goes the JSON string" placeholder and worst of all Set ScriptEngine = New ScriptControl was compiling *only* if Microsoft Script Control 1.0 is added in Project->References menu (not Components) which is non-obvious.

    I personally wouldn't load the whole JScript engine to parse some tiny JSON snippet only. Try [VB6/VBA] JSON parsing to built-in VBA.Collections for one light-weight solution.

    cheers,
    </wqw>

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width