Function JSONtoXML(jsonText)
Dim idx, max, ch, mode, xmldom, xmlelem, xmlchild, name, value
Set xmldom = CreateObject("Microsoft.XMLDOM")
xmldom.loadXML "<xml/>"
Set xmlelem = xmldom.documentElement
max = Len(jsonText)
mode = 0
name = ""
value = ""
While idx < max
idx = idx + 1
ch = Mid(jsonText, idx, 1)
Select Case mode
Case 0 ' Wait for Tag Root
Select Case ch
Case "{"
mode = 1
End Select
Case 1 ' Wait for Attribute/Tag Name
Select Case ch
Case """"
name = ""
mode = 2
Case "{"
Set xmlchild = xmldom.createElement("tag")
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
Case "["
Set xmlchild = xmldom.createElement("tag")
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
Case "}"
Set xmlelem = xmlelem.parentNode
Case "]"
Set xmlelem = xmlelem.parentNode
End Select
Case 2 ' Get Attribute/Tag Name
Select Case ch
Case """"
mode = 3
Case Else
name = name + ch
End Select
Case 3 ' Wait for colon
Select Case ch
Case ":"
mode = 4
End Select
Case 4 ' Wait for Attribute value or Tag contents
Select Case ch
Case "["
Set xmlchild = xmldom.createElement(name)
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
name = ""
mode = 1
Case "{"
Set xmlchild = xmldom.createElement(name)
xmlelem.appendChild xmlchild
xmlelem.appendchild xmldom.createTextNode(vbCrLf)
xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
Set xmlelem = xmlchild
name = ""
mode = 1
Case """"
value = ""
mode = 5
Case " "
Case Chr(9)
Case Chr(10)
Case Chr(13)
Case Else
value = ch
mode = 7
End Select
Case 5
Select Case ch
Case """"
xmlelem.setAttribute name, value
mode = 1
Case "\"
mode = 6
Case Else
value = value + ch
End Select
Case 6
value = value + ch
mode = 5
Case 7
If Instr("}], " & Chr(9) & vbCr & vbLf, ch) = 0 Then
value = value + ch
Else
xmlelem.setAttribute name, value
mode = 1
Select Case ch
Case "}"
Set xmlelem = xmlelem.parentNode
Case "]"
Set xmlelem = xmlelem.parentNode
End Select
End If
End Select
Wend
Set JSONtoXML = xmlDom
End Function
'read data from client
records = Request.Form("records")
'convert the JSON string to an array
Set oRegExpre = new RegExp
oRegExpre.Global = true
oRegExpre.Pattern = "[\[\]\{\}""]+"
records = replace(records, "},{","||")
records = oRegExpre.Replace(records, "" )
aRecords = split(records,"||")
'iterate the array and do some cleanup
for each rec in aRecords
aRecord = split(rec,",")
id = split(aRecord(1),":")(1)
field = split(aRecord(0),":")(0)
updateValue = split(aRecord(0),":")(1)
updateValue = replace(updateValue,chr(10),"\n")
updateValue = replace(updateValue,chr(13),"\r")
updateValue = replace(updateValue,"'","''")
'etc
next