Published on InterSystems Developer Community (https://community.intersystems.com)

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

Question
Yone Moreno · 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?

#Code Snippet #Caché

Source URL:https://community.intersystems.com/post/how-could-we-convert-string-object-simpler-way