O
O
Orkhan Hasanli2022-01-14 01:42:56
JavaScript
Orkhan Hasanli, 2022-01-14 01:42:56

How to map json to an array of objects?

Good day!
I am writing a small application and I need to map json containing an array of objects into an array of objects. In Java, this is handled through ModelMapper, MapStruct, etc. But I don’t know how to implement this in ts / js.
json comes in response to a post request made via axios.

let routingRequest: RoutingRequest = new RoutingRequest(coordinates);
axios({
    method: 'post',
    url: routingUrl,
    data: routingRequest,
    headers: {
        'Content-Type': 'application/json'
    }
}).then(function (response: AxiosResponse) {
    let routes: Route[] = [];
    response.data.forEach(
        (route: any) => {
            routes.push(route as Route)
        }
    )
}).catch(function (error: AxiosError) {
    console.log(error);
});

Example json that comes in:
[
    {
        "instructions": [
        {
            "turnDescription": "Продолжите движение",
            "distance": 130.26291709756794,
            "time": 23447,
            "sign": 0,
            "name": "",
            "points": [
            {
                "latitude": 53.901085505509265,
                "longitude": 27.55511741648531
            },
            {
                "latitude": 53.9010582,
                "longitude": 27.5552493
            },
            {
                "latitude": 53.9013356,
                "longitude": 27.556052
            }]
        }],

        "description": [],
        "distance": 2887.358604882371,
        "ascend": 0.0,
        "descend": 0.0,
        "routeWeight": 506.1757242634384,
        "time": 304025,
        "pointsOrder": []
    }
]


Models I want to use:
export class Route {

    private instructions: Instruction[];
    private description: number[];
    private distance: number;
    private ascend: number;
    private descend: number;
    private routeWeight: number;
    private time: number;
    private pointsOrder: number[];

    // constructors, getters, setters

}

export class Instruction {

    private turnDescription: string;
    private distance: number;
    private time: number;
    private sign: number;
    private name: string;
    private points: Point[];

    // constructors, getters, setters

}

export class Point {

    private latitude: number;
    private longitude: number;

    // constructors, getters, setters

}


When I try to deserialize into an array of objects, I get either an error in the catch branch e is undefined, or just undefined

Unfortunately, I could not find the necessary information on the Internet
. I would be glad for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2022-01-14
@azerphoenix

Just:

let routes = await axios(...)
.then((response: AxiosResponse<Route[]>) => response.data)
.catch(console.log);
It makes no sense to drive from empty to empty.
Just keep in mind that typescript does all type and model checks at compile time. At runtime, no typescript exists, so when a response comes from the server, there are no guarantees that it matches the model, and typescript will not be able to catch it if it does not. If you need any checks, you need to write them yourself with your own hands or with the help of the appropriate libs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question