2

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":"example.webhook@mandrillapp.com",
     "sender":"example.sender@mandrillapp.com",
     "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":"example.webhook@mandrillapp.com",
     "sender":"example.sender@mandrillapp.com",
     "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
  • 16,398
  • 7
  • 76
  • 175
4532066
  • 2,042
  • 5
  • 21
  • 48
  • This question has already been asked - http://stackoverflow.com/q/30538292/692942. just the OP hasn't bothered to accept an answer so I can't flag this as a duplicate. – user692942 Jun 11 '15 at 14:00
  • @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 – 4532066 Jun 11 '15 at 15:06
  • @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. – Dijkgraaf Jun 15 '15 at 00:59
  • @Dijkgraaf It does now so I've flagged it, I couldn't very well upvote my own answer could I?? – user692942 Jul 08 '15 at 08:45
  • 1
    @StacPollaidh The exact same applies, just wrap the data from mandrill in a containing object `{data: [] };`. – user692942 Jul 08 '15 at 08:47

1 Answers1

1

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
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54