Obtain value of a JSON object according to the value of another JSON object

4

Transfondo:

I have an application in VB.NET where I consume some web services "API's". One of these APIs (for the purposes of the question will be called EnviarArchivo ) returns an object with this structure:

API response EnviarArchivo :

[{
    "DocumentType": "SalesInvoice",
    "CreationDate": "2019-01-02T15:26:00.041Z",
    "DocumentDate": "2018-10-17T00:00:00",
    "DueDate": "2018-10-17T00:00:00.000Z",
    "Currency": "COP",
    "BusinessStatus": "Certified",
    "CommunicationStatus": "DeliverOk",
    "MainNotificationEmailStatus": "Delivered",
    ...
}]

I have a global variable called "translationRespuestasAPI" whose value is a JSON object with this structure:

Global variable traduccionRespuestasAPI :

var traduccionRespuestasAPI = {
    "Cancel": "Cancelar",
    "Categories": "Categorias",
    "Certified": "Certificado",
    "CitizenshipCard": "Tarjeta Ciudadanía",
    "Delireved": "Entregado",
    "Delivered": "Entregado",
    "DeliverOk": "Entrega Exitosa",
    "Description_Label": "Descripción",
    "Received": "Recibido",
    "Registered": "Registrado",
    "Reject": "Rechazar",
    ...
};

Problem:

What I'm looking for is that "according to the response of the API EnviarArchivo " see the global variable traduccionRespuestasAPI and set the value.

Example:

If the response of the API EnviarArchivo in the field "BusinessStatus" was "Certified", you should search the global variable traduccionRespuestasAPI the attribute with name "Certified" and show the value "Certificate".

Without using loops, how to access the value of the local variable according to the response of the API?

    
asked by Mauricio Arias Olave 02.01.2019 в 19:25
source

1 answer

4

Assuming that the value of the API response will always be present in your translations object, you can do it like this:

var EnviarArchivo = [{
    "DocumentType": "SalesInvoice",
    "CreationDate": "2019-01-02T15:26:00.041Z",
    "DocumentDate": "2018-10-17T00:00:00",
    "DueDate": "2018-10-17T00:00:00.000Z",
    "Currency": "COP",
    "BusinessStatus": "Certified",
    "CommunicationStatus": "DeliverOk",
    "MainNotificationEmailStatus": "Delivered"
}];

var traduccionRespuestasAPI = {
    "Cancel": "Cancelar",
    "Categories": "Categorias",
    "Certified": "Certificado",
    "CitizenshipCard": "Tarjeta Ciudadanía",
    "Delireved": "Entregado",
    "Delivered": "Entregado",
    "DeliverOk": "Entrega Exitosa",
    "Description_Label": "Descripción",
    "Received": "Recibido",
    "Registered": "Registrado",
    "Reject": "Rechazar"
};

console.log(traduccionRespuestasAPI[EnviarArchivo[0]["BusinessStatus"]]);
    
answered by 02.01.2019 / 19:35
source