19

I've got a rapid development tool for an ERP-system, which allows only vbscript. I'm trying to create a simple AJAX-Request with VBS. That worked with the "Microsoft.XMLHTTP"-object.

Next step is to receive data from a webserver using json. But in VBS there seems to be no function like "json_decode" oder other.

Does anyone know a solution? Or is the only option to develop my own json-function?

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
Stefan Brendle
  • 1,545
  • 6
  • 20
  • 39

5 Answers5

14

As JSON is a hierarchical data format, using Regular expressions and Split(), as Peter proposed, won't get you far.

If your environment allows CreateObject() you may be able to use a ready made COMponent written in another language (e.g. wrap the standard json2.js in a .WSC or COM enable a .NET DLL). Another option would be to harness another language via the Microsoft Script Control. The con of this approach is that you'll have to deal with the objects/arrays delivered by the other language (some hints are to be found in the topic Peter refered to).

A pure VBScript solution can be found here. I can't read the documentation, but the code compiles and 'works' for simple test cases - YMMV.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • The Solution from demon.tw works great, but the performance is very slow. I've got a simple "Array" with 10-15 simple values. One single array contains the information of one product. I've got 3500 products, so I have to run the JSON-encoding 3500x times. For this task it tasks 2-3 minutes. Without JSON-Encoding it runs fast, but I'll need this JSON-format. Is there a possiblity to speed it up? 30 Seconds would be acceptable. – Stefan Brendle Sep 26 '12 at 09:32
  • I've solved the problem by building the JSON Syntax manually. I've just joined the strings itself and wrapped them with JSON-Syntax and escaped some special chars. So the performance is great - and works in my case. – Stefan Brendle Nov 30 '12 at 10:11
  • _e.g. wrap the standard json2.js in a .WSC or COM enable a .NET DLL_ Can you please point me to website or any documentation on how to do it? – Gurmanjot Singh Feb 09 '18 at 18:54
  • Thanks Ekkehard this solution worked for me. I was thinking you should have pasted the code here because in the future that domain might go missing and the solution would be lost? My json code was different to their example, but instead of changing their code I just modified my json response to match the syntax. Where mine started with [] brackets so I just put outside of it some extra tagging like this: {"results":[ ... ]} I just wanted to say thanks for sharing – Rosski Dec 08 '22 at 13:54
11

How about doing this with ASPJSON?
Available from http://www.aspjson.com/

I'm about to use this as a solution for a very old site to send an ajax call (using Jquery) with the encoded data to a MongoDB, for testing.

Logan
  • 388
  • 2
  • 9
4

I had a similar problem so I wrote a JSONtoXML function in VBScript for one of my projects. No warranties on this script (it's provided as-is and has known limitations such as not handling all types of escape sequences):

Const stateRoot = 0
Const stateNameQuoted = 1
Const stateNameFinished = 2
Const stateValue = 3
Const stateValueQuoted = 4
Const stateValueQuotedEscaped = 5
Const stateValueUnquoted = 6
Const stateValueUnquotedEscaped = 7

Function JSONToXML(json)
  Dim dom, xmlElem, i, ch, state, name, value
  Set dom = CreateObject("Microsoft.XMLDOM")
  state = stateRoot
  For i = 1 to Len(json)
    ch = Mid(json, i, 1)
    Select Case state
    Case stateRoot
      Select Case ch
      Case "["
        If dom.documentElement is Nothing Then
          Set xmlElem = dom.CreateElement("ARRAY")
          Set dom.documentElement = xmlElem
        Else
          Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
        End If
      Case "{"
        If dom.documentElement is Nothing Then
          Set xmlElem = dom.CreateElement("OBJECT")
          Set dom.documentElement = xmlElem
        Else
          Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
        End If
      Case """"
        state = stateNameQuoted 
        name = ""
      Case "}"
        Set xmlElem = xmlElem.parentNode
      Case "]"
        Set xmlElem = xmlElem.parentNode
      End Select
    Case stateNameQuoted 
      Select Case ch
      Case """"
        state = stateNameFinished
      Case Else
        name = name + ch
      End Select
    Case stateNameFinished
      Select Case ch
      Case ":"
        value = ""
        State = stateValue
      End Select
    Case stateValue
      Select Case ch
      Case """"
        State = stateValueQuoted
      Case "{"
        Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
        State = stateRoot
      Case "["
        Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
        State = stateRoot
      Case " "
      Case Chr(9)
      Case vbCr
      Case vbLF
      Case Else
        value = ch
        State = stateValueUnquoted
      End Select
    Case stateValueQuoted
      Select Case ch
      Case """"
        xmlElem.setAttribute name, value
        state = stateRoot
      Case "\"
        state = stateValueQuotedEscaped
      Case Else
        value = value + ch
      End Select
    Case stateValueQuotedEscaped ' @@TODO: Handle escape sequences
      value = value + ch
      state = stateValueQuoted
    Case stateValueUnquoted
      Select Case ch
      Case "}"
        xmlElem.setAttribute name, value
        Set xmlElem = xmlElem.parentNode
        state = stateRoot
      Case "]"
        xmlElem.setAttribute name, value
        Set xmlElem = xmlElem.parentNode
        state = stateRoot
      Case ","
        xmlElem.setAttribute name, value
        state = stateRoot
      Case "\"
         state = stateValueUnquotedEscaped
      Case Else
        value = value + ch
      End Select
    Case stateValueUnquotedEscaped ' @@TODO: Handle escape sequences
      value = value + ch
      state = stateValueUnquoted
    End Select
  Next
  Set JSONToXML = dom
End Function

Function XMLCreateChild(xmlParent, tagName)
  Dim xmlChild
  If xmlParent is Nothing Then
    Set XMLCreateChild = Nothing
    Exit Function
  End If
  If xmlParent.ownerDocument is Nothing Then
    Set XMLCreateChild = Nothing
    Exit Function
  End If
  Set xmlChild = xmlParent.ownerDocument.createElement(tagName)
  xmlParent.appendChild xmlChild
  Set XMLCreateChild = xmlChild
End Function
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75
  • Thanks for the great function Stephen. Please review below project that originated adapting your idea. https://github.com/pravynandas/JSONToXML – PravyNandas Sep 14 '17 at 09:42
1

Check out https://github.com/rcdmk/aspJSON

Not sure if this has any relation to www.ASPJSON.com (now defunct) mentioned in Logan's answer.

Keith
  • 20,636
  • 11
  • 84
  • 125
0

You should better roll out your own based on a query here on json and asp. Like eg this one Any good libraries for parsing JSON in Classic ASP? Most of the time json2 library is used but this is based on jscript so no option for you. Also most of the time this kind of JSON has a fixed structure so it should not be so difficult to parse with a Regularexpression like i demonstrated in a few answer like the one above. You could publish some of your JSON so that we can test it with some routines.

Community
  • 1
  • 1
peter
  • 41,770
  • 5
  • 64
  • 108