0

Im trying to parse a json string in classic asp. A found a useful script referenced on stackoverflow: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

when calling the 'parse' function I get an error:

<script language="JScript" runat="server" src="json2.js"></script>

'line 144 of my script


Set myJSON = JSON.parse(versionsStr)


Microsoft VBScript runtime error '800a01a8' 

Object required: '[string: "[{"self":"https://dc"]' 

/libs/asp/common/jira_api.asp, line 144 

Here is my json string (versionStr):

 [{"self":"https://server.com:8343/rest/api/2/version/10300","id":"10300","description":"baseline version","name":"1.0","archived":false,"released":true,"releaseDate":"2013-11-07","userReleaseDate":"07/Nov/13"},{"self":"https://server.com:8343/rest/api/2/version/10301","id":"10301","description":"sample version","name":"1.1.0","archived":false,"released":false},{"self":"https://server.com:8343/rest/api/2/version/10302","id":"10302","name":"3.0.0","archived":false,"released":false}]

Im guessing that the script Im using is expecting a json object, but my calls to the jira API are returing json strings. Does someone have a solution that parses a json string into a classic asp array?

Stuart
  • 711
  • 1
  • 11
  • 35
prufrock
  • 239
  • 1
  • 4
  • 15

1 Answers1

1

You are right. You can call javascript code from vbscript code. And you can use the json2.js script, BUT what you can not do is pass arrays from javascript to vbscript and use indexed access to them.

Save this as test.wsf and run with cscript test.wsf (make sure json2.js is in the same directory).

<?XML version="1.0" standalone="yes" encoding="utf-8" ?>
<package>
<job id="test" prompt="no">
<?job error="true" debug="true" timeout="10" ?>

<script language="Javascript" src="json2.js" />

<script id="main" language="VBScript">
<![CDATA[

    Dim versionStr
        versionStr ="[{""self"":""https://server.com:8343/rest/api/2/version/10300"",""id"":""10300"",""description"":""baseline version"",""name"":""1.0"",""archived"":false,""released"":true,""releaseDate"":""2013-11-07"",""userReleaseDate"":""07/Nov/13""}" + _ 
                    ",{""self"":""https://server.com:8343/rest/api/2/version/10301"",""id"":""10301"",""description"":""sample version"",""name"":""1.1.0"",""archived"":false,""released"":false}" + _ 
                    ",{""self"":""https://server.com:8343/rest/api/2/version/10302"",""id"":""10302"",""name"":""3.0.0"",""archived"":false,""released"":false}]"

    Dim myJSON 
        Set myJSON = JSON.parse(versionStr)

    Dim numVersions
        numVersions = myJSON.length

    Dim i, version
        For i = 1 to numVersions
            Set version = myJSON.shift()
            WScript.Echo version.self
        Next


]]>
</script>

</job>
</package>
MC ND
  • 69,615
  • 8
  • 84
  • 126