How parse this from grpc service to table component?

Hi i got the following grpc api answer:

{
    "records": [
        {
            "id": "0",
            "record": {
                "idclave_nombre": " ALEXIS 28-10-16-K",
                "__dozer_record_version": 1,
                "ubicacion": "EE",
                "nombres": "ALEXIS MIGUEL",
                "apellidos": "MARCHADO",
                "cedula": "8-712-1266",
                "sector": "E PRIVADA",
                "empresa": "TRANSPORTE Y MULTISERVICIOS,S.A.  ( DON LEE )",
                "motivo": "ATENDIDO",
                "fecha_nacimiento": "1971-11-12",
                "sexo": "M"
            }
        },
        {
            "id": "1",
            "record": {
                "idclave_nombre": " FRANCISCO 31-8-11",
                "__dozer_record_version": 1,
                "ubicacion": "CERRADO",
                "nombres": "FRANCISCO",
                "apellidos": "ROMERO CEDEÑO",
                "cedula": "8-766-118",
                "sector": "E PRIVADA",
                "empresa": "CONALVIA",
                "acreedor": "INSTACREDIT",
                "analista": "JHANMARY",
                "sucursal": "AVE PERU",
                "motivo": "ATENDIDO",
                "ps": "S",
                "fecha_nacimiento": "1983-01-05",
                "sexo": "M"
            }
        }
    ]
}

how i can parse that to the table component in retool? i need some kind of transformation?

thank you

Hello @agaitan026 ,

To parse the gRPC API answer, you can use the following code:

const response = {
  records: [
    {
      id: "0",
      record: {
        idclave_nombre: " ALEXIS 28-10-16-K",
        __dozer_record_version: 1,
        ubicacion: "EE",
        nombres: "ALEXIS MIGUEL",
        apellidos: "MARCHADO",
        cedula: "8-712-1266",
        sector: "E PRIVADA",
        empresa: "TRANSPORTE Y MULTISERVICIOS,S.A.  ( DON LEE )",
        motivo: "ATENDIDO",
        fecha_nacimiento: "1971-11-12",
        sexo: "M"
      }
    },
    {
      id: "1",
      record: {
        idclave_nombre: " FRANCISCO 31-8-11",
        __dozer_record_version: 1,
        ubicacion: "CERRADO",
        nombres: "FRANCISCO",
        apellidos: "ROMERO CEDEÑO",
        cedula: "8-766-118",
        sector: "E PRIVADA",
        empresa: "CONALVIA",
        acreedor: "INSTACREDIT",
        analista: "JHANMARY",
        sucursal: "AVE PERU",
        motivo: "ATENDIDO",
        ps: "S",
        fecha_nacimiento: "1983-01-05",
        sexo: "M"
      }
    }
  ]
};

// Parse the response to an array of objects
const parsedResponse = response.records.map(record => record.record);

// Print the parsed response
console.log(parsedResponse);

Output:

[
  {
    idclave_nombre: " ALEXIS 28-10-16-K",
    __dozer_record_version: 1,
    ubicacion: "EE",
    nombres: "ALEXIS MIGUEL",
    apellidos: "MARCHADO",
    cedula: "8-712-1266",
    sector: "E PRIVADA",
    empresa: "TRANSPORTE Y MULTISERVICIOS,S.A.  ( DON LEE )",
    motivo: "ATENDIDO",
    fecha_nacimiento: "1971-11-12",
    sexo: "M"
  },
  {
    idclave_nombre: " FRANCISCO 31-8-11",
    __dozer_record_version: 1,
    ubicacion: "CERRADO",
    nombres: "FRANCISCO",
    apellidos: "ROMERO CEDEÑO",
    cedula: "8-766-118",
    sector: "E PRIVADA",
    empresa: "CONALVIA",
    acreedor: "INSTACREDIT",
    analista: "JHANMARY",
    sucursal: "AVE PERU",
    motivo: "ATENDIDO",
    ps: "S",
    fecha_nacimiento: "1983-01-05",
    sexo: "M"
  }
]

You can then use the parsed response in your application however you need.

:grinning:

Patrick

1 Like

The thing the response came from gRPC query is not fixed like its in code presented. Its dynamic so should edit that transformer code i think the first part as it comes from lets say query1.data


i had tried this

return data.records.map(o => {
  return {
    "idclave_nombre": o.record.idclave_nombre
  }
});

but no chance to map it automatically without having to insert each field by separate?

@agaitan026 Hi there, I went ahead and double checked this for you. You can use the script below to transform your data into an array of flat objects:

var result = [];
formatDataAsArray(data).forEach(record => {
    var flatObj = {};
    var obj = record.records.record;
    flatObj.id = record.records.id;
    for (const key in obj) flatObj[key] = obj[key];
    result.push(flatObj);
});
return result;

You can use this in the Transform Results section of your query. Please note the formatDataAsArray function which performs an initial transformation of the data into an array of objects prior to the flattening process:

And now when you link the data to your table it will appear as flat tabular data:

I hope this works for you, but let me know if you have any questions.