· May 17, 2017

XML to JSON in Ensemble

Hi Team,

We were able to convert XML file to JSON using Studio and terminal, but kindly let us know how this conversion occurs using Ensemble production.


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

I would imagine that the same code you wrote to convert XML to JSON in studio could be used from an Ensemble production to do the conversion from within the context of a production.  You could implement this code as a business operation method and could pass a message containing the XML as the request message and then the response message would contain the json converted version.

Can you share your code that converts XML to JSON from Terminal?


Class MyApp.Person Extends (%Persistent, %XML.Adaptor, %RegisteredObject, MyApp.Address)

Parameter XMLNAME = "Person";

Property Name As %String;

Property DOB As %String;

Property Address As MyApp.Address;

Method Import() As %String
    Set reader = ##class(%XML.Reader).%New() 
    Set status = reader.OpenFile("C:\InterSystems\Ex.xml")
    If $$$ISERR(status) {do $System.Status.DisplayError(status)}
    Do reader.Correlate("Person","MyApp.Person")
    if $$$ISERR(status) {do $System.Status.DisplayError(status) quit $$$ERROR()}
    While (reader.Next(.object,.status)) {
       set ..Name = object.Name
       set ..DOB = object.DOB
       set ..Address.City = object.Address.City
       set ..Address.Zip = object.Address.Zip
       set ..City = object.Address.City
       set ..Zip = object.Address.Zip
        Write object.Name,!
        Write object.DOB,!
        write object.Address.City, !
        write object.Address.Zip
    If $$$ISERR(status) {do $System.Status.DisplayError(status)}
    Quit $$$OK

Method GetTarget(ByRef pParameters As %String, Output pObject As %RegisteredObject) As %Status
  Set pObject = ##class(MyApp.Person).%New()
  set ins = pObject.Import()
  set status = pObject.%Save()
   set writer=##class(%XML.Writer).%New()
    set writer.Indent=1
    set statuswriter=writer.OutputToFile("C:\InterSystems\Outfile5.txt")
    set st = writer.StartWrite()
  set status2 =##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONFromObject(writer,pObject,,,,"cbwi")
  Quit $$$OK



Class MyApp.Address Extends (%SerialObject, %Populate, %Persistent, %XML.Adaptor)

Property City As %String;

Property Zip As %String;