Question
· Sep 11, 2020

How could we convert a string to an object in a simpler way?

Hello

We were wondering if there is a more concise and simple way to convert information from a string to an object.

The string has each line separated by ; and each field divided by |

The string would look like:

fecha1|nombreProfesional1|centro1|secuencia1|hora1|bloque1|acto1|lugar1|duracion1|nombreOrigen1|modo1||codigoCentro1|codigoAgenda1|especialidad1|codigoCentroOrigen1|codigoAgendaOrigen|borrable|telefono1|telefono2||origen1|;fecha1|nombreProfesional1|centro1|secuencia1|hora1|bloque1|acto1|lugar1|duracion1|nombreOrigen1|modo1||codigoCentro1|codigoAgenda1|especialidad1|codigoCentroOrigen1|codigoAgendaOrigen|borrable|telefono1|telefono2||origen1|;

We would need to convert the previous string to a list of the following object:

Class EsquemasDatos.DragoAP.ConsultarCitas Extends (%Persistent, %XML.Adaptor, %RegisteredObject)
{


Property fecha As %String(MAXLEN = "");


Property nombreProfesional As %String(MAXLEN = "");


Property centro As %String(MAXLEN = "");


Property secuencia As %String(MAXLEN = "");


Property hora As %String(MAXLEN = "");


Property bloque As %String(MAXLEN = "");


Property acto As %String(MAXLEN = "");


Property lugar As %String(MAXLEN = "");


Property duracion As %String(MAXLEN = "");


Property nombreOrigen As %String(MAXLEN = "");


Property modo As %String(MAXLEN = "");


Property codigoCentro As %String(MAXLEN = "");


Property codigoAgenda As %String(MAXLEN = "");


Property especialidad As %String(MAXLEN = "");


Property codigoCentroOrigen As %String(MAXLEN = "");


Property codigoAgendaOrigen As %String(MAXLEN = "");

Property tipoProfesional As %String(MAXLEN = "");

Property borrable As %String(MAXLEN = "");

Property telefono1 As %String(MAXLEN = "");

Property telefono2 As %String(MAXLEN = "");

Property origen As %String(MAXLEN = "");

 

We have been able to convert it with the following function:

ClassMethod citaPreviaConsultarCitasResponse(citas As %String(MAXLEN="")) As Mensajes.Response.DragoAP.ConsultarCitasResponse
{

 
    set response = ##class(Mensajes.Response.DragoAP.ConsultarCitasResponse).%New()
    
    set datos = ##class(%ListOfDataTypes).%New()
    
    
    //Get each line which is ended by ;
    while (($find(citas,";")>0) && (citas'="")){
      set cita = $piece(citas,";",1)

      set citas = $extract(citas,$find(citas,";"),*)
      
      
      set dato = ##class(EsquemasDatos.DragoAP.ConsultarCitas).%New()
      
      set dato.fecha = $piece(cita,"|",1)
      set dato.nombreProfesional = $piece(cita,"|",2)
      set dato.centro = $piece(cita,"|",3)
      set dato.secuencia = $piece(cita,"|",4)
      set dato.hora = $piece(cita,"|",5)
      set dato.bloque = $piece(cita,"|",6)
      set dato.acto = $piece(cita,"|",7)
      set dato.lugar = $piece(cita,"|",8)
      set dato.duracion = $piece(cita,"|",9)
      set dato.nombreOrigen = $piece(cita,"|",10)
      set dato.modo = $piece(cita,"|",11)
      set dato.codigoCentro = $piece(cita,"|",13)
      set dato.codigoAgenda = $piece(cita,"|",14)
      set dato.especialidad = $piece(cita,"|",15)
      set dato.codigoCentroOrigen = $piece(cita,"|",16)
      set dato.codigoAgendaOrigen = $piece(cita,"|",17)
      set dato.borrable = $piece(cita,"|",18)
      set dato.telefono1 = $piece(cita,"|",19)
      set dato.telefono2 = $piece(cita,"|",20)
      set dato.origen = $piece(cita,"|",21)
      
      do datos.Insert(dato)
      
    }
    
    set response.datos = datos
    quit response
}

 

Which is being called from a Bussiness Process

 

➡️ Would you mind to share how would you code this task?

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

At first sight, the sequence of your values matches the sequence of your properties.

transforming your input from 

fecha1|nombreProfesional1|centro1|secuencia1|hora1|bloque1|acto1|lug....

to

'fecha1','nombreProfesional1','centro1','secuencia1','hora1','bloque1','acto1','lug....

in an exercise for beginners.
Next  I would use SQL to insert the record

INSERT  INTO  
EsquemasDatos_DragoAP.ConsultarCitas (fecha1,nombreProfesional1,centro1,secuencia1,hora1,bloque1,acto1,lug....)

VALUES ('fecha1','nombreProfesional1','centro1','secuencia1','hora1','bloque1','acto1','lug....)
 

An that's it!

Listing of the columns is only necessary if you distrust the class compiler
or if you want to prevent problems with later sequence changes in the class definition.
In any case, it should reflect the sequence of your data input, independent of sequence in class.

I'd suggest to use one of the ResultSet classes and avoid embedded SQL.
The whole SQL statement can be composed as a single string  and then processed .

more on INSERT and variations