Convert %String withn JSON structure to %DynamicObject and then to XML message

Primary tabs

Hello,

 

We have been reasearching how could we parse a string with a JSON structure like the following:

 

{"code":200,"type":"Consulta","message":"Operacion correcta","result":[{"id":38802130,"paciente":null,"prestacion":{"codigo":"1.52","descripcion":"Extracción ordinaria","tipo":null},"agenda":{"codigo":"TLAB","descripcion":"LABORATORIO CAE TELDE","sala":""},"servicio":{"codigo":"EXTIN","descripcion":"Unidad de Extracciones Insular"},"centroDerivacion":{"codigo":null,"descripcion":null},"centro":{"codigo":"350290","descripcion":"CHUIMI"},"medico":"Nombre4251 Apellido1_4251 Apellido2_4251","fechaHora":"'26/03/2020' '08:07'","estado":"","tipoLista":"","fechaGrabacion":"","circustanciaCita":"","fechaTurno":"","estadoTurno":"","fechaDeriviacionCentro":"","ubicacion":""},{"id":6817873,"paciente":null,"prestacion":{"codigo":"1.52","descripcion":"Extracción ordinaria","tipo":null},"agenda":{"codigo":"EXTRACCION","descripcion":"EXTRACCIONES LABORATORIO","sala":""},"servicio":{"codigo":"EXT","descripcion":"Extracciones"},"centroDerivacion":{"codigo":null,"descripcion":null},"centro":{"codigo":"350252","descripcion":"HGF"},"medico":"Nombre356 Apellido1_356 Apellido2_356","fechaHora":"'31/03/2020' '08:26'","estado":"","tipoLista":"","fechaGrabacion":"","circustanciaCita":"","fechaTurno":"","estadoTurno":"","fechaDeriviacionCentro":"","ubicacion":""}]}

 

To a XML message, like the following:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <code>200</code>
   <message>Operacion correcta</message>
   <result>
      <element>
         <agenda>
            <codigo>TLAB</codigo>
            <descripcion>LABORATORIO CAE TELDE</descripcion>
            <sala />
         </agenda>
         <centro>
            <codigo>350290</codigo>
            <descripcion>CHUIMI</descripcion>
         </centro>
         <centroDerivacion>
            <codigo null="true" />
            <descripcion null="true" />
         </centroDerivacion>
         <circustanciaCita />
         <estado />
         <estadoTurno />
         <fechaDeriviacionCentro />
         <fechaGrabacion />
         <fechaHora>'26/03/2020' '08:07'</fechaHora>
         <fechaTurno />
         <id>38802130</id>
         <medico>Nombre4251 Apellido1_4251 Apellido2_4251</medico>
         <paciente null="true" />
         <prestacion>
            <codigo>1.52</codigo>
            <descripcion>Extracción ordinaria</descripcion>
            <tipo null="true" />
         </prestacion>
         <servicio>
            <codigo>EXTIN</codigo>
            <descripcion>Unidad de Extracciones Insular</descripcion>
         </servicio>
         <tipoLista />
         <ubicacion />
      </element>
      <element>
         <agenda>
            <codigo>EXTRACCION</codigo>
            <descripcion>EXTRACCIONES LABORATORIO</descripcion>
            <sala />
         </agenda>
         <centro>
            <codigo>350252</codigo>
            <descripcion>HGF</descripcion>
         </centro>
         <centroDerivacion>
            <codigo null="true" />
            <descripcion null="true" />
         </centroDerivacion>
         <circustanciaCita />
         <estado />
         <estadoTurno />
         <fechaDeriviacionCentro />
         <fechaGrabacion />
         <fechaHora>'31/03/2020' '08:26'</fechaHora>
         <fechaTurno />
         <id>6817873</id>
         <medico>Nombre356 Apellido1_356 Apellido2_356</medico>
         <paciente null="true" />
         <prestacion>
            <codigo>1.52</codigo>
            <descripcion>Extracción ordinaria</descripcion>
            <tipo null="true" />
         </prestacion>
         <servicio>
            <codigo>EXT</codigo>
            <descripcion>Extracciones</descripcion>
         </servicio>
         <tipoLista />
         <ubicacion />
      </element>
   </result>
   <type>Consulta</type>
</root>

 

We have tested using the terminal that we can convert the string to %DynamicObject, which then can be printed as JSON directly:

 

set cita = {"code":200,"type":"Consulta","message":"Operacion correcta","result":[{"id":38802130,"paciente":null,"prestacion":{"codigo":"1.52","descripcion":"Extracción ordinaria","tipo":null},"agenda":{"codigo":"TLAB","descripcion":"LABORATORIO CAE TELDE","sala":""},"servicio":{"codigo":"EXTIN","descripcion":"Unidad de Extracciones Insular"},"centroDerivacion":{"codigo":null,"descripcion":null},"centro":{"codigo":"350290","descripcion":"CHUIMI"},"medico":"Nombre4251 Apellido1_4251 Apellido2_4251","fechaHora":"'26/03/2020' '08:07'","estado":"","tipoLista":"","fechaGrabacion":"","circustanciaCita":"","fechaTurno":"","estadoTurno":"","fechaDeriviacionCentro":"","ubicacion":""},{"id":6817873,"paciente":null,"prestacion":{"codigo":"1.52","descripcion":"Extracción ordinaria","tipo":null},"agenda":{"codigo":"EXTRACCION","descripcion":"EXTRACCIONES LABORATORIO","sala":""},"servicio":{"codigo":"EXT","descripcion":"Extracciones"},"centroDerivacion":{"codigo":null,"descripcion":null},"centro":{"codigo":"350252","descripcion":"HGF"},"medico":"Nombre356 Apellido1_356 Apellido2_356","fechaHora":"'31/03/2020' '08:26'","estado":"","tipoLista":"","fechaGrabacion":"","circustanciaCita":"","fechaTurno":"","estadoTurno":"","fechaDeriviacionCentro":"","ubicacion":""}]}

 

write $CLASSNAME(cita)
%Library.DynamicObject

 

 

do cita.%ToJSON()
{"code":200,"type":"Consulta","message":"Operacion correcta","result":[{"id":38802130,"paciente":null,"prestacion":{"codigo":"1.52","descripcion":"Extracción ordinaria","tipo":null},"agenda":{"codigo":"TLAB","descripcion":"LABORATORIO CAE TELDE","sala":""},"servicio":{"codigo":"EXTIN","descripcion":"Unidad de Extracciones Insular"},"centroDerivacion":{"codigo":null,"descripcion":null},"centro":{"codigo":"350290","descripcion":"CHUIMI"},"medico":"Nombre4251 Apellido1_4251 Apellido2_4251","fechaHora":"'26/03/2020' '08:07'","estado":"","tipoLista":"","fechaGrabacion":"","circustanciaCita":"","fechaTurno":"","estadoTurno":"","fechaDeriviacionCentro":"","ubicacion":""},{"id":6817873,"paciente":null,"prestacion":{"codigo":"1.52","descripcion":"Extracción ordinaria","tipo":null},"agenda":{"codigo":"EXTRACCION","descripcion":"EXTRACCIONES LABORATORIO","sala":""},"servicio":{"codigo":"EXT","descripcion":"Extracciones"},"centroDerivacion":{"codigo":null,"descripcion":null},"centro":{"codigo":"350252","descripcion":"HGF"},"medico":"Nombre356 Apellido1_356 Apellido2_356","fechaHora":"'31/03/2020' '08:26'","estado":"","tipoLista":"","fechaGrabacion":"","circustanciaCita":"","fechaTurno":"","estadoTurno":"","fechaDeriviacionCentro":"","ubicacion":""}]}

 

 

However, how could we convert 🔁 the %DynamicObject to a XML message?

 

We have read:

  1. https://cedocs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cl...
  2. https://community.intersystems.com/post/json-changes-cach%C3%A9-20162
  3. https://community.intersystems.com/post/alternative-zenauxiliaryaltjsonp...
  4. https://cedocs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cl...
  5. https://community.intersystems.com/post/json-xml

 

 

 

 

Replies

Hi,

i would start with the xml-structure you expect and which probably/hopefully is already defined by an xml-schema.

You can import xml-schemas into IRIS to generate XML-enabled classes which extends %XML.Adaptor.

https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=GX...

If you do not have a xml-schema, you need to create it or your xml-enabled classes manually. (extending %XML.Adaptor)

https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=GX...

If you have that ready, let your XML-enabled classes also extend %JSON.Adaptor.

https://docs.intersystems.com/irislatest/csp/docbook/Doc.View.cls?KEY=GJ...

Use %JSON.Adaptor to import the JSON data (i think DynamicObject is also supported here directly)
into your XML enabled class with the help of %JSONImport() method.

Then use XMLExport..() method (inherited from %XML.Adaptor) to export as XML data.

I have not tried this so far but i think it should work :)

HTH,
Bernd