juane1711
juane1711

Reputation: 1

How to use LazyList's toJson method with json arrays?

I'm doing a one to many association with two tables: "usuarios" and "rol" here there is no problem, the issue is that I'm using the function "toJson" to convert the result of the consult to a String with json format, just like this:

[
  {
    "activo":1,
    "alta":null,
    "contrasenia":"AA123.aa",
    "edicion":null,
    "editor_alta":null,
    "editor_edicion":null,
    "email":"dadadada@ssfsfds",
    "id_division":0,
    "id_pools":1,
    "id_roles":2,
    "id_usuarios":1234567895,
    "nombre":"Javier",
    "primer_apellido":"a",
    "segundo_apellido":"a",
    "parents":{
      "rols":[
        {
          "activo":1,
          "alta":"2019-07-08",
          "concepto":"Administrador Pool",
          "descripcion":"Usuario que gestiona a los agentes",
          "edicion":null,
          "editor_alta":1111111111,
          "editor_edicion":null,
          "id_permisos":2,
          "id_roles":2
        }
      ]
    }
  }
]

But i only want to get some of the params in the json, for example I only want to have the "id_usuarios" and the "parents{rols[conecepto]}" params, so I indicate in the toJson method the name of this params but the result json is something like this:

[
  {
    "id_usuarios":1234567895,
    "concepto":null,
    "parents":{
      "rols":[
        {
          "activo":1,
          "alta":"2019-07-08",
          "concepto":"Administrador Pool",
          "descripcion":"Usuario que gestiona a los agentes",
          "edicion":null,
          "editor_alta":1111111111,
          "editor_edicion":null,
          "id_permisos":2,
          "id_roles":2
        }
      ]
    }
  }
]

As you can see it is not obtaining the "concepto" param, and it is including all the params of "parents".

So, is there any way to get just some parameters?, and how can i access to the values of the parents{rols} parameters?

I am using this code to create the json string

LazyList<Usuarios> usuarios = Usuarios.where("activo = 1").include(Rol.class);
        String json = usuarios.toJson(true, "id_usuarios","concepto");
``


Upvotes: 0

Views: 82

Answers (1)

ipolevoy
ipolevoy

Reputation: 5518

The LazyList.toJson() documentation can be found here: http://javalite.github.io/activejdbc/snapshot/org/javalite/activejdbc/LazyList.html#toJson-boolean-java.lang.String...-

Looks like you are getting the correct result. If your concepto = null then maybe it is in fact NULL in the database? Did you look into that?

Also, the attribute list is applied to the current model, and not to children or parents, which means that under parents node you are going to get all attributes.

Upvotes: 0

Related Questions