Looping through JSON using ASPJSON

10,598

Wrap the str2 so that it is a valid JSON collection and update your code to iterate that collection as below.

<!--#INCLUDE file="aspJSON.asp" -->
str2 = <POST DATA FROM MANDRILL>

' Wrap str2 to turn it into a collection
str2 = "{""events"":[" & str2 & "]}"

Set oJSON = New aspJSON
oJSON.loadJSON(str2)

response.write "<h1>test</h1>"

For Each eventrec In oJSON.data("events") ' Iterate through events
    Set this = oJSON.data("events").item(eventrec).item("msg") ' select msg in each eventrec
    Response.Write _
    this.item("subject") & ": " & _
    this.item("diag") & "<br>"
Next
Share:
10,598
4532066
Author by

4532066

Updated on June 04, 2022

Comments

  • 4532066
    4532066 almost 2 years

    I am using Classic ASP and ASPJSON (http://www.aspjson.com/) to try to loop through JSON test data returned via a Mandrill Webhook.

    This is the sample JSON data - I realise the 2nd block is the same as the first, but I need to use it since when I do this on the live system the webhook data will be returned in batches in a single JSON file / post.

    {
      "event":"hard_bounce",
      "msg":{
         "ts":1365109999,
         "subject":"This an example webhook message",
         "email":"[email protected]",
         "sender":"[email protected]",
         "tags":[
            "webhook-example"
         ],
         "state":"bounced",
         "metadata":{
            "user_id":111
         },
         "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
         "_version":"exampleaaaaaaaaaaaaaaa",
         "bounce_description":"bad_mailbox",
         "bgtools_code":10,
         "diag":"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
      },
      "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
      "ts":1433940242
    },
    {
      "event":"hard_bounce",
      "msg":{
         "ts":1365109999,
         "subject":"This an example webhook message",
         "email":"[email protected]",
         "sender":"[email protected]",
         "tags":[
            "webhook-example"
         ],
         "state":"bounced",
         "metadata":{
            "user_id":111
         },
         "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
         "_version":"exampleaaaaaaaaaaaaaaa",
         "bounce_description":"bad_mailbox",
         "bgtools_code":10,
         "diag":"smtp;550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces."
      },
      "_id":"exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
      "ts":1433940242
    }
    

    In my test ASP page, I tried a simple test (I have already confirmed the JSON data is valid by saving it to a database and checking the content)

    <!--#INCLUDE file="aspJSON.asp" -->
    str2 = <POST DATA FROM MANDRILL>
    Set oJSON = New aspJSON
    oJSON.loadJSON(str2)
    
    Response.Write oJSON.data("event") & "<hr>"
    response.write "<h1>test</h1>"
    
    For Each phonenr In oJSON.data("msg")
        Set this = oJSON.data("msg").item(phonenr)
        Response.Write _
        this.item("subject") & ": " & _
        this.item("diag") & "<br>"
    Next
    

    When Mandrill calls the ASP page, it returns this error:

    Description: Object not a collection : .

    For this line: For Each phonenr In oJSON.data("msg")

    I am stuck working out how, for each event, I can loop through it and get the attributes from "msg" and the "_id" value as well.

    • user692942
      user692942 almost 9 years
      This question has already been asked - stackoverflow.com/q/30538292/692942. just the OP hasn't bothered to accept an answer so I can't flag this as a duplicate.
    • 4532066
      4532066 almost 9 years
      @Lankymart thanks for your reply. Looking at the other post you linked to, your answer for how to get at, in that example, PitcherID, was to ask if the OP had access to control the JSON structure. In my case I don't have access to do that as I am taking the data straight from Mandrill. As that's the case, is there any way I can access the data in the two blocks on JSON data? Thanks
    • Dijkgraaf
      Dijkgraaf almost 9 years
      @Lankymart For a duplicate it needs to either have an accepted answer or an upvoted one. So if you think a answer on the other question is correct, upvote it and then mark as duplicate.
    • user692942
      user692942 almost 9 years
      @Dijkgraaf It does now so I've flagged it, I couldn't very well upvote my own answer could I??
    • user692942
      user692942 almost 9 years
      @StacPollaidh The exact same applies, just wrap the data from mandrill in a containing object {data: [] };.