Question
· May 24, 2019

How to copy array values from JSon to object using DTL?

Hi community,

I need to create a DTL to get values from a JSon (%DynamicObject) to a class.

Thre first items is copied fine, however when it tries to get the values from an array it raises an error.

 

{
  "resultado": "OK",
  "retorno": {
    "mensaje": "Info recuperado correctamente",
    "datos": {
      "idUsuario": "ID-88556",
      "fechanacimiento": "1970-02-01",
      "nombreUsuario": "FRANCISCO",
      "apellidosUsuario": "LOPEZ",
      "polizas": {
        "poliza": [
          {
            "sucursal": "MAD",
            "oficina": "01",
            "poliza": "12345678",
          },
          {
            "sucursal": "BCN",
            "oficina": "34",
            "poliza": "99556151",
          }
        ]
      }
    }
  }
}

When the DTL gets the "poliza" array, it throws the following error.

ERROR <Ens>ErrException: <INVALID OREF>zTransform+16^MyApp.DT.JsonToPoliciesResponse.1 -- - registrado como '-' número - @' Set k1=source.retorno.datos.polizas.poliza.Next(k1)' 

is there any way to create a "loop", "for each" or whatever to iterate into?

 

Best regard,

Francisco Lopez

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

Hi Francisco,

You will need to create an iterator from the poliza %DynamicArray property, e.g.

set iterator=source.retorno.datos.polizas.poliza.%GetIterator()


You can then do a %GetNext(.key,.value) over the iterator where value will be a %DynamicObject that has the properties sucursal etc.

The other option would be to use the ZEN jsonProvider to deserialise the entire object for you, bypassing the need for the DTl.

Francisco,

instead of FOR EACH which obviously uses a nonexisting   Next method
you could create a loop using <WHILE> and do a "manual" loop.

For the condition   source.retorno.datos.polizas.poliza.%Size() gives you the  index limit

and source.retorno.datos.polizas.poliza.%Get(idx).sucursal   .... and similar provides the content. 

You have to increment idx manually and it runs from 0 (zero!) to %Size()-1

It's not as elegant as <foreach> but you have control over your JSON input.