12

Sample Code:

Dim myObject
Set myObject = JSON.parse(someJsonResponseFromTheServer)    

myFunction(myObject.someProperty)

The Problem:

When code similiar to this is ran in my application, it throws a 500 error from the server with a message similar to "Object Does not support property or method 'someProperty'. What I would like to do to solve this problem is something like this:

Dim myObject
Set myObject = JSON.parse(someJsonResponseFromTheServer)    

If myObject.someProperty Then
    myFunction(myObject.someProperty)
End If

However, if I add the conditional, it throws the same error on the line with the conditional instead of the line with the method call.

My Question:

In ASP Classic, how do you detect if a property exists within an object without throwing an error?

Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115
  • Natively ASP does not have a json parser. What are your using to get a JSON implementation into ASP? – AnthonyWJones Apr 16 '12 at 11:53
  • @AnthonyWJones, Good question. Given that I just started this project (and job) like 3 days ago, I'm not quite sure how they are doing it. – Levi Hackwith Apr 16 '12 at 13:20
  • There are two JSON for ASP Classic implementations. From json.org: [aspjson](http://code.google.com/p/aspjson/) and [JSON ASP utility class](http://www.webdevbros.net/2007/04/26/generate-json-from-asp-datatypes/). The last one is no longer maintained. – Guido Gautier Apr 17 '12 at 09:23
  • @AnthonyWJones Like VBScript, JavaScript can also be run on the server side just like Node.js using the classic ASP engine. – devlord Feb 25 '13 at 17:58

3 Answers3

17

One of the benefits of classic ASP is that it allows you to run both VBScript and JScript in the same page - thus you can use the power of both.

First, add this block of JScript code to your existing .asp file:

<script language="JScript" runat="server">
function CheckProperty(obj, propName) {
    return (typeof obj[propName] != "undefined");
}
</script>

And assuming VBScript is the default language in the page, you can call it from within your VBScript code like this:

Dim myObject
Set myObject = JSON.parse(someJsonResponseFromTheServer)    
If CheckProperty(myObject, "someProperty") Then
    myFunction(myObject.someProperty)
End If

Tested it with generic class object and it works fine - the JScript is compiled before the VBScript thus accessible to it.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • Be carefull though, combining JavaScript and VBScript. Take a look at [this](http://stackoverflow.com/questions/10083632/call-a-function-written-on-vb-from-a-jscript-code-classic-asp) SO thread for some very important things to note. – Guido Gautier Apr 17 '12 at 09:25
  • @Guido as long as VBScript is the default it should work just fine: "The non-default language scripts are parsed and executed first... By the time the default language script is parsed and executed all the global identifiers created by the previous language scripts **will have been added to the script environment and are therefore available for use from inline code**" – Shadow The GPT Wizard Apr 17 '12 at 10:36
  • Shadow Wizard, thanks, this will indeed work perfect. I wanted to point Levi on possible issues when combining the two. – Guido Gautier Apr 17 '12 at 10:38
  • 1
    Six years later and this is still gold. Thank you! – Phellipe Ribeiro Jul 04 '18 at 23:13
7

Sadly, this usually means an 'on error' statement.

Private Function TestProperty()
    Dim Success
    Success = False

    On Error Resume Next
        ' set for property here
        Success = (Err.Number = 0)
    On Error Goto 0
    TestProperty = Success
Exit Function   
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
5

On the assumption that you are including some runat="server" js file that provides you with the JSON parser then JSON.parse is going to return a Javascript object.

If the above assumption is correct then the following ought to work:

If myObject.hasOwnProperty("someProperty") Then 
    myFunction(myObject.someProperty) 
End If 
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306