Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

Convert JSON to XML

You may come across situation when you receive data in JSON and would like to convert into XML. JSON (JavaScript Object Notation) is a text-based open standard designed for human-readable data interchange.

 

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML.

 

Following example is a function which converts a JSON data string into XML Document.

 

Private Shared Function JsonToXml(ByVal json As String) As XmlDocument
        Dim newNode As XmlNode = Nothing
        Dim appendToNode As XmlNode = Nothing
        Dim returnXmlDoc As XmlDocument = New XmlDocument
        returnXmlDoc.LoadXml("<Document />")
        Dim rootNode As XmlNode = returnXmlDoc.SelectSingleNode("Document")
        appendToNode = rootNode
 
        json = json.Replace(",", "," & vbLf)
        json = json.Replace("{", "{" & vbLf)
        json = json.Replace("}", vbLf & "}" & vbLf)
        json = json.Replace("[", "[" & vbLf)
        json = json.Replace("]", vbLf & "]" & vbLf)
 
        Dim arrElements() As String = json.Split(CChar(vbLf))
        For Each element As String In arrElements
            Dim processElement As String = element.Replace(vbLf, "").Replace(vbCr, "").Replace(vbTab, "").Trim()
            If processElement.Trim <> "" Then
                If (processElement.IndexOf("}") > -1 Or processElement.IndexOf("]") > -1) And Not (appendToNode Is rootNode) Then
                    appendToNode = appendToNode.ParentNode
                ElseIf (processElement.IndexOf("[") > -1) Then
                    processElement = processElement.Replace(":", "").Replace("[", "").Replace("""", "").Trim()
                    If processElement.Trim = "" Then
                        processElement = "Square"
                    End If
                    newNode = returnXmlDoc.CreateElement(processElement)
                    appendToNode.AppendChild(newNode)
                    appendToNode = newNode
                ElseIf (processElement.IndexOf("{") > -1) Then
                    processElement = processElement.Replace(":", "").Replace("{", "").Replace("""", "").Trim()
                    If processElement.Trim = "" Then
                        processElement = "Element"
                    End If
                    newNode = returnXmlDoc.CreateElement(processElement)
                    appendToNode.AppendChild(newNode)
                    appendToNode = newNode
                Else
                    Dim FoundAt As Integer = processElement.IndexOf(":")
                    If (FoundAt > -1) Then
                        Dim NodeName As String = processElement.Substring(0, FoundAt - 1).Replace("""", "")
                        Dim NodeValue As String = processElement.Substring(FoundAt + 1, processElement.Length - FoundAt - 1).Trim
                        If NodeValue.StartsWith("""") Then
                            NodeValue = NodeValue.Substring(1, NodeValue.Length - 1).Trim
                        End If
                        If NodeValue.EndsWith(",") Then
                            NodeValue = NodeValue.Substring(0, NodeValue.Length - 1).Trim
                        End If
                        If NodeValue.EndsWith("""") Then
                            NodeValue = NodeValue.Substring(0, NodeValue.Length - 1).Trim
                        End If
                        newNode = returnXmlDoc.CreateElement(NodeName)
                        newNode.InnerText = NodeValue
                        appendToNode.AppendChild(newNode)
                    End If
                End If
            End If
        Next
        Return returnXmlDoc
    End Function
Loading