Question
· Apr 1, 2017

XML to Json conversion

Hello,

Are there any utilities/api in HealthConnect 2016.2.1 that will allow conversion of XML virtual document to Json format? We were thinking to convert HL7 ADT message to XML via Ensemble DTL and then send it to another BP to convert to Json format for transmission via Web Services. To my understanding there isn't anyway to represent Json as a virtual object so it can be use for direct mapping of HL7 2.x message to Json via Ensemble DTL.

Thanks

Yuriy

Discussion (3)1
Log in or sign up to continue


Hi Yuriy,

Currently there are no API's to convert XML to JSON but it can be done quite easily in different ways.

One approach you can use is if your XML has always the same structure, you can create a class(es) in HS that corresponds to this XML structure. With this class(es) you can now use %XML.Reader and method Correlate() to load the XML into the class(es). Here you can find more information about this:
http://docs.intersystems.com/ens20162/csp/docbook/DocBook.UI.Page.cls?KEY=GXML_import

After correlating the XML to your class you should then use method %WriteJSONStreamFromObject from class %ZEN.Auxiliary.jsonProvider to dump this object in JSON format into a stream. Documentation is here:
http://docs.intersystems.com/ens20162/csp/documatic/%25CSP.Documatic.cls?PAGE=CLASS&LIBRARY=%25SYS&CLASSNAME=%25ZEN.Auxiliary.jsonProvider#%WriteJSONStreamFromObject
There is also a string version of this method but your JSON will need to fit in up to 3.5MB.

This is the simplest approach but it relies on being able to represent your XMLs as a class (or classes) in HS.

Another approach is to use some external third-party library to convert XML to JSON. There are several open source libraries for Java, .Net, Python, and others.

Hi Yuriy,

Not so far ago I was need such utilit, and i didn't find it, so I've written simple method, that converts xml-node from DOM-represented xml, may be it'll be usefull for you:

/// Метод генерации динамичекского объекта из ноды
/// Подчеркивания и дефисы в названиях тегов удаляются
ClassMethod NodeToDynObj(Node As %XML.Node) As %DynamicObject
{
 d Node.MoveToFirstChild(1)
 i (Node.NodeType = $$$xmlTEXTNODE) {
    s res = Node.NodeData
    d Node.MoveToParent(1)
    q res
 }
 s res = {}
 do {
     s propertyName = $TR(Node.LocalName,"-_","")
     continue:propertyName=""

     i ((Node.HasChildNodes(1)) && (Node.NodeType '=$$$xmlWHITESPACENODE) && (Node.NodeType '=$$$xmlTEXTNODE)){
         s value = ..NodeToDynObj(Node)
         d res.%Set(propertyName,value)
     }
     else{
          s value = ""
          d Node.GetText(.value)
          d res.%Set(propertyName,value)
     }
 } while Node.MoveToNextSibling(1)

 d Node.MoveToParent(1)
 q res
}