Amendment: Some people have had trouble downloading the aspJSON.asp file from the site where I had found it. You can download the file from me.
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
Sometimes you get trapped in using languages you don’t prefer. I often find my self playing in the bowels of Classic ASP. Now Classic ASP is not bad, but it is a little outdated and has not really been built upon for a few years. I have recently built some new tools in Classic ASP to compliment an existing tool set; however, I tricked out ASP with JSON.
To do this I had found a wonderful import file written by Gerrit van Kuipers. You can download a copy of the file from: www.aspjson.com. Yes it is version 1.0, but I have not found an issue with it yet.
At the very bottom of this article is the the site listed again along with a tool to validate JSON strings.
You will need to import the aspJSON.asp file in your ASP file.
<!--#include virtual="/aspJSON.asp" -->
To read the JSON request you will need to read the binary of the request and convert it to a string.
If Request.TotalBytes > 0 Then Dim lngBytesCount lngBytesCount = Request.TotalBytes jsonstring = BytesToStr(Request.BinaryRead(lngBytesCount)) End If Function BytesToStr(bytes) Dim Stream Set Stream = Server.CreateObject("Adodb.Stream") Stream.Type = 1 'adTypeBinary Stream.Open Stream.Write bytes Stream.Position = 0 Stream.Type = 2 'adTypeText Stream.Charset = "iso-8859-1" BytesToStr = Stream.ReadText Stream.Close Set Stream = Nothing End Function
Next you will need to create the JSON object to parse it.
Set oJSON = New aspJSON oJSON.loadJSON(jsonstring)
Now you can access the data using the JSON object you created in the above step.
FirstName = oJSON.data.item("FirstName")
To Write a JSON request you will need to do the following.
You will need to import the aspJSON.asp file in your ASP file.
<!--#include virtual="/aspJSON.asp" -->
Next you will need to create the JSON object and populate it
Set oJSON = New aspJSON set oJSON.data("employee") set newitem = oJSON.addToCollection(oJSON.data("employee")) newitem.add "FirstName", "Andrew" newitem.add "LastName", "Pallant"
Now you can submit your request
Response.Clear Response.ContentType = "application/json" Response.Write oJSON.JSONoutput()
You can vist: http://www.aspjson.com/ for complete example and to download the aspJSON.asp file.
A tool that I use to validate the string file is: http://jsonformatter.curiousconcept.com/
Part II of the Article Has Just Been Added: http://andrewpallant.ca/wordpress/tricking-classic-asp-json-mandrill-experience/
Hey, great tutorial. However I’m confused as how to include the JSON file. Could you point for me where do we suppose to import the JSON file in the script?
downnload the aspJSON.asp file from http://www.aspjson.com. This file is what you need to parse the JSON request in ASP. assuming you put the aspJSON in the same folder as the file you are working with you would use the following notation:
<!–#include virtual="/aspJSON.asp" –>
Follow my blog from this point onward. It is pretty complete in how to use the file to parse the JSON request.
In the html file making the JSON request, it is same as any other JSON call. Feel free to ping me with any other questions.
Using the original code for a JSON string of
{ “redirectURL”: “REDIRECT_URL”, “key”: “GOOGLE_APP_ID”, “longUrl”: “MEN_URL” }
I get this error on the line ” Stream.Write bytes”
ADODB.Stream error ‘800a0bb9’
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Any ideas? I need to read an incoming body containing a json string using classic asp.
/stripe/stripe.asp, line 36
I assume you are getting the error in the BytesToStr function?
This would fail if the bytes being read is incomplete or 0.
Ensure you are using the following:
If Request.TotalBytes > 0 Then
Dim lngBytesCount
lngBytesCount = Request.TotalBytes
jsonstring = BytesToStr(Request.BinaryRead(lngBytesCount))
End If
Hi and thanks for the post!,I’m trying to get a JSON response calling an url from inside my ASP file,then work with the date that the call gives me,how can i do that call? I’m just 100% new to JSOn and I can’t figure it out(ie,how to create the json object in asp,do the call,etc. Thanks in advanced.
contact me through my full site at http://www.unlatched.com. I will help you as much as I can.
hi, can you show an example of reading/updating a JSON collection?
Can you elaborate more on what you are looking for? I would be happy to write a little piece of code example for you.
Hello,
Can you tell me how to loop through several top level items? I am trying to parse this JSON from SendGrid. http://sendgrid.com/docs/API_Reference/Webhooks/event.html.
The farthest that I have gotten is this
For Each subItem In oJSON.data
B = B & subItem & vbNewLine
Next
I get a listing 1 to 23 (collection in the parent collection).
Thanks.
THANKS FOR POSTING THIS !! Was really helpful !!
I’m trying to do the same with SendGrid, did you ever figure this out?
I have just done a solution for Mandrill and the concept should be similar for SendGrid. I will try to do a new post tonight for you regarding the Mandrill. Please check back.
If the Mandrill solution doesn’t work for you in SendGrid, please let me know.
Checking back, did you ever get time for the Mandrill post? I’m actually going to switch to Mandrill so that info would save me a ton of time.
Hi Chris M – I will be writing the post tonight with Sample Code. I got buried alive with work last week. I will send you an email directly with the URL for you.
very good
I’m trying to run this on IIS 7 giving me a 500 – Internal server error.
What I’m trying to achieve is read a json file from the same folder as the asp file that’s running it.
And I’ve read that Msxml2.ServerXMLHTTP cannot run and read files from the same physical server. Is this true? Are there any solutions that you can suggest.
@kbstlnt. Error 500 is a generic message that does not provide any details. If you are using IE, turn off the friendly error messages in the advanced tab of the properties window. You will then get a more detailed error with line a number.
hello Erik, did what you said. it displays only this one:
Server Error
500 – Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
It looks like you need to also enable errors in IIS as well. Running a quick search for something like “iis 7 detailed error pages” will get you some instructions on how to turn that on.
– Erik
plese help me… How write with aspjson (http://www.aspjson.com/) the json below:
[
{
“file_name”: “xx”,
“user_name”: “xxx”,
“action_date”: “xxx”,
“action_text”: “xxx”,
“user_profile_photo”: “xxx”
},
{
“file_name”: “yyy”,
“user_name”: “yyy”,
“action_date”: “yyy”,
“action_text”: “yyy”,
“user_profile_photo”: “yyy”
}
]
this my json how to read
{“data”:[{“message”:”is there any surprise for me?”,”created_time”:”2015-10-08T05:49:09+0000″,”id”:”412741762152133_1039612999465003″},{“created_time”:”2015-10-05T08:09:23+0000″,”id”:”412741762152133_1038062752953361″,”link”:”https:\/\/www.facebook.com\/412741762152133\/photos\/a.1038062796286690.1073741828.412741762152133\/1038062752953361\/?type=3″},{“created_time”:”2015-10-05T08:04:53+0000″,”id”:”412741762152133_1038062032953433″},{“message”:”I am happy but don’t know how much long Exist”,”created_time”:”2015-10-05T07:55:32+0000″,”id”:”412741762152133_1038059242953712″},{“message”:”i am waiting for vs2015″,”created_time”:”2015-10-05T06:01:26+0000″,”id”:”412741762152133_1038035432956093″},{“created_time”:”2013-06-06T06:34:26+0000″,”id”:”412741762152133_487241871368788″,”link”:”https:\/\/www.facebook.com\/412741762152133\/photos\/a.487241434702165.1073741826.412741762152133\/487241858035456\/?type=3″},{“message”:”photo”,”created_time”:”2013-06-06T06:34:23+0000″,”id”:”412741762152133_487241421368833″,”link”:”https:\/\/www.facebook.com\/412741762152133\/photos\/a.487241398035502.1073741824.412741762152133\/487241421368833\/?type=3″},{“created_time”:”2013-06-06T06:32:34+0000″,”id”:”412741762152133_487241444702164″,”link”:”https:\/\/www.facebook.com\/412741762152133\/photos\/a.487241434702165.1073741826.412741762152133\/487241438035498\/?type=3″}],”paging”:{“previous”:”https:\/\/graph.facebook.com\/v2.0\/412741762152133\/feed?fields=message,created_time,id,link&since=1444283349&access_token=CAACEdEose0cBAAXmXnmOW0EnQ8ghYceunZA57R2BZBloFWGHijZBIYz9rmLoVQ2tJYjlNvaoHKwrAUZAoiIfA6myF8VQlfnDyCMRxFB6YqaFdVikZAr8SXJBoxOsrPe9COedi1Xl8Hj0gecM2uq69glaZAHPpBDcRZA6YMx2iJXXq61VW61SVvZBb3rqk2SmQ8Kb37HNMyRhAAZDZD&limit=25&__paging_token=enc_AdAq3MZAS0Bk8gAYOoiJECcCpAjPGwSeq5PssnIEkyP9VaM7NZAWIEsO3ZAcizLJb4gzUs1ZAIUgVusd4xwweTrdKcWJH2DDOufcoLPJPgrNzzDMZBgZDZD&__previous=1″,”next”:”https:\/\/graph.facebook.com\/v2.0\/412741762152133\/feed?fields=message,created_time,id,link&access_token=CAACEdEose0cBAAXmXnmOW0EnQ8ghYceunZA57R2BZBloFWGHijZBIYz9rmLoVQ2tJYjlNvaoHKwrAUZAoiIfA6myF8VQlfnDyCMRxFB6YqaFdVikZAr8SXJBoxOsrPe9COedi1Xl8Hj0gecM2uq69glaZAHPpBDcRZA6YMx2iJXXq61VW61SVvZBb3rqk2SmQ8Kb37HNMyRhAAZDZD&limit=25&until=1370500354&__paging_token=enc_AdBfOvKiUg4A2yxkwZBD65FaXnZCy43OkB1guIxZBisaNQSQD5GBfVLZCHRcUZCspUtNHL4R7HZALRaGiShixM67jZBB8ZAjsBDfcm3PgCNSJDWjpfa2LgZDZD”}}
@devin The example on the aspjason.com site is pretty straight forward. The tick is that you need to know what you are looking for and where it is in the data. If there is a spot where you are stuck at, post that code.