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.
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!
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
Hi Mac;
The way I would call a REST WebService ( JSON ) in Classic ASP is to do something like:
Set HttpReq = Server.CreateObject(“MSXML2.ServerXMLHTTP”)
HttpReq.open “GET”, “www.url.com”, False
HttpReq.send
If you have to send data; looking into something similar, but it will be doing a PUT instead of a GET/
Through a quick Google search I have found the following resources that I believe would help you.
Integrating ASP.NET XML Web Services with ‘Classic’ ASP Applications
Consuming a WSDL Webservice from ASP
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: https://andrewpallant.ca/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 )
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
Dear Andrew,
Thank you so much!
Highest Regards,
Mac.