Tricking Out Classic ASP With JSON – The Mandrill Experience

Using JSON with Classic ASP can be a tricky thing, but the developer of ASPJSON has made it very easy.  This library ( or include ) has made my life so much easier, but the instructions to get me started was somewhat raw and difficult for me to follow.

Since my original posting about  Tricking Out Classic ASP with JSON, I have had several people ask for the solution to SendGrid and Mandrill.  Both systems are very different, but the principals are similar.  I found Mandrill the easiest to implement.

The following code is the magic to it all, but this example is specific to Mandrill.

   jsonstring = ""  

   If Request.TotalBytes > 0 Then   
     Dim lngBytesCount   
     lngBytesCount = Request.TotalBytes   
     jsonstring = BytesToStr(Request.BinaryRead(lngBytesCount))   
   End If   

   Set oJSON = New aspJSON  
   'Load JSON string  
   jsonstring = replace(jsonstring, "mandrill_events=","")  
   oJSON.loadJSON(URLDecode(jsonstring))  

 ' Loop Through Records  
 for i = 0 to oJSON.data.count -1  
   str = oJSON.data(i).item("event")  
   straddress = oJSON.data(i).item("msg").item("email")  
 next  

You can download the full code sample from:  http://www.unlatched.com/sample/Mandrill%20with%20Classic%20ASP/mandrill%20aspjson%20testing.zip

I did include the ASPJSON include file so that you would get the complete solution; but you should ensure you have the latest once you have my example working.  Goto ASPJSON to get the latest library.

Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http://www.unlatched.com/#sthash.8DiTkpKy.dpuf

Posted in ASP, JSON, Web Tagged with: , , , ,
10 comments on “Tricking Out Classic ASP With JSON – The Mandrill Experience
  1. John says:

    Hi, Thanks for posting about this. I’m using this for a SendGrid application. Do you know if there is a way to extract the key names from the JSON? I don’t always know what the item names will be. Given this JSON

    [
    {
    “email”: “john.smith@sendgrid.com”,
    “timestamp”: 1386636127,
    “uid”: “123456”,
    “ip”: “174.127.33.234”,
    “purchase”: “PO1452297845”,
    “useragent”: “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36”,
    “id”: “001”,
    “category”: [“category1″,”category2″,”category3”],
    “event”: “open”
    },
    {
    “uid”: “123456”,
    “ip”: “174.56.33.234”,
    “purchase”:”PO1452297845″,
    “useragent”:”Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36″,
    “event”:”click”,
    “email”: “john.doe@sendgrid.com”,
    “timestamp”:1386637216,
    “url”:”http://www.google.com/”,
    “category”:[“category1″,”category2″,”category3”],
    “id”:”001″
    }
    ]

    id, purchase, uid, and url are all what SendGrid calls unique identifier. They could be anything or not in there at all.

    I can get the values by iterating through the object. oJSON.data(0).item(0) would return the email value “john.smith@sendgrid.com” I believe, but I would like to also include the name of the key “email”, and any other keys that may be there. Do you know if there is a way to do this? I’m somewhat new to ASP and JSON. Thanks!

    • Chris says:

      John,
      Did you ever figure this out? Coming up against the same thing in Mandrill.

      Thanks
      Chris

      • Hi Chris;

        The solution is as outlined in: http://andrewpallant.com/wordpress/tricking-classic-asp-json-mandrill-experience/

        I have had feed back from 2 people letting me know it worked well for them.

        Was there a specific issue you were having? I may be able to point you in the correct direction.

        Kind Regards

        Andrew ( LdnDeveloper )

        • Chris says:

          Andrew,
          I think that I’m missing something that’s really apparent but I’m not getting it. Right now I’m able to loop through the json and get the values. The issue I’m having now is that an item might not exist and it get an error because it can’t be found. What I need to do is check and make sure the item exists in json before trying to get the data out.

          Example, oJSON.data(i).item(“location”).item(“country_short”) the item location -> country_short does not exist in the json but almost every time it does but in this specific case it didn’t and my page will through an error. So what I need to do is something like:

          If exists location_country_short = oJSON.data(i).item(“location”).item(“country_short”) then do this and if not skip it.

          Does that make sense?

          Thanks
          Chris

          • Hi Chris;

            I believe I know the answer, but I am going to sit down tonight and work out an example to send you.

            My email is andrewpallant@gmail.com. If you can send me your email to my email, I will respond to you directly.

            Kind Regards

            Andrew

          • The JSON.data is essentially a dictionary. Since it is a dictionary, you can use calls like exists which will return you true or false.

            The following is an example of how you might use it.

            if oJSON.data(i).exists(“location”) then
            ‘ Get Location Info
            if oJSON.data(i).item(“location”).exists(“country_short”) then
            str = oJSON.data(i).item(“location”).item(“country_short”)
            end if
            end if

  2. Machiel van Rheenen says:

    Hello Andrew,

    Thank you for your knowledge sharing.
    I’m a bit out of my league on a job I have to do and I am hoping you will assist me some more to get me gooing.
    It concerns a classic ASP generaties website.
    I have to synchronise database info from two separate databases when updating on our website.
    There is this HTTP API I can Use to request data and update it on the remote database.
    I.e.: Https//www.remotesite.nl/remoteapi.asp?apikey=qwerty&action=ha_get&id=123456&date=2014-5-4
    It returns a JSON formatted string
    Question:
    After updating our database. In need to do iT again throug the api on the remote site.
    Im thinking using a server.execute to a .asp file that i call after the local update.
    The .asp would call the api and decode the json.
    On returning from the server.execute a stored session variable would be used to examin/communicate the synchronisation result.

    Just dont know how to call a Url from within a .ASP and parse the json string it returns. Doh.

    Highest regards,

    Mac

  3. Luis Felipe says:

    {“orders”:[{“code”:”Test-5″,”placed_at”:”2015-10-23T13:31:10-02:00″,”updated_at”:”2015-10-23T13:31:10-02:00″,”total_ordered”:95.0,”shipping_cost”:5.0,”estimated_delivery”:”2015-11-07T13:31:10-02:00″,”shipping_address”:{“street”:”Av. das Am\u00e9ricas”,”number”:”700″,”detail”:””,”neighborhood”:”Barra da Tijuca”,”city”:”Rio de Janeiro”,”region”:”RJ”,”country”:”BR”,”postcode”:”22640100″},”billing_address”:{“street”:”Av. das Am\u00e9ricas”,”number”:”700″,”detail”:””,”neighborhood”:”Barra da Tijuca”,”city”:”Rio de Janeiro”,”region”:”RJ”,”country”:”BR”,”postcode”:”22640100″},”customer”:{“name”:”SkyHub Sistemas de Integra\u00e7\u00e3o”,”email”:”naoinformado@skyhub.com.br”,”vat_number”:”19760622000146″,”phones”frown-emoticon”(21) 3490-3811″]},”items”:[{“id”:”00000002″,”name”:”Colch\u00e3o D45 Ortobom”,”qty”:1,”original_price”:100.0,”special_price”:90.0}],”status”:{“code”:”13″,”label”:”Pagamento Confirmado”,”type”:”APPROVED”},”invoices”:[],”shipments”:[]},{“code”:”Test-4″,”placed_at”:”2015-10-23T13:31:08-02:00″,”updated_at”:”2015-10-23T13:31:08-02:00″,”total_ordered”:1420.0,”shipping_cost”:5.0,”estimated_delivery”:”2015-11-07T13:31:08-02:00″,”shipping_address”:{“street”:”Av. das Am\u00e9ricas”,”number”:”700″,”detail”:””,”neighborhood”:”Barra da Tijuca”,”city”:”Rio de Janeiro”,”region”:”RJ”,”country”:”BR”,”postcode”:”22640100″},”billing_address”:{“street”:”Av. das Am\u00e9ricas”,”number”:”700″,”detail”:””,”neighborhood”:”Barra da Tijuca”,”city”:”Rio de Janeiro”,”region”:”RJ”,”country”:”BR”,”postcode”:”22640100″},”customer”:{“name”:”SkyHub Sistemas de Integra\u00e7\u00e3o”,”email”:”naoinformado@skyhub.com.br”,”vat_number”:”19760622000146″,”phones”frown-emoticon”(21) 3490-3811″]},”items”:[{“id”:”1027_s_131″,”name”:”Colch\u00e3o Probel Pocket Cielo -Casal-1,38×1,88×0,28″,”qty”:1,”original_price”:1730.0,”special_price”:1415.0}],”status”:{“code”:”13″,”label”:”Pagamento Confirmado”,”type”:”APPROVED”},”invoices”:[],”shipments”:[]}],”total”:5}

    Set oJSON = New aspJSON
    jsonstring = xmlhttp.responseText
    oJSON.loadJSON(jsonstring)

    set pedidos = oJSON.data(“orders”)

    For Each codigo In pedidos
    response.write “”
    Set this = pedidos.item(codigo)
    Response.Write “”&this.item(“code”) & “”
    Response.Write “”&this.item(“placed_at”) & “”
    Response.Write “”&this.item(“total_ordered”) & “”
    Response.Write “”&this.item(“shipping_address”)(“street”) & “, “&this.item(“shipping_address”)(“number”) & ” “&this.item(“shipping_address”)(“detail”) & “”

    Response.Write “”&this.item(“shipping_address”)(“city”) & “”
    Response.Write “”&this.item(“shipping_address”)(“region”) & “”
    Response.Write “”&this.item(“shipping_address”)(“post_code”) & “”

    response.write “”

    Next

    How do I list ITEMS make up items ?

    Items need to capture the next request and may have 1, 2 or more different items within the [ ]

1 Pings/Trackbacks for "Tricking Out Classic ASP With JSON – The Mandrill Experience"
  1. […] Amendment:: Some people were asking for a solution to Mandrill and SendGrid. Both solutions are similar and therefore I selected one to publish. You can contact me if you would like more. See the following: Part II – The Mandrill Experience […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*