D
D
dc65k2021-05-01 21:39:56
Java
dc65k, 2021-05-01 21:39:56

How to do the following data transformation in Java?

Hello.
There is an initial List:
Order.java

package main.java;

import java.util.ArrayList;
import java.util.List;

public class Order {

    private String date;

    private String docTypesName;

    private String docId;

    private String image;

    private String name;

    private double price;

    private int quantity;

    private int removed;

    public Order(String date, String docTypesName, String docId, String image, String name, double price, int quantity, int removed) {
        this.date = date;
        this.docTypesName = docTypesName;
        this.docId = docId;
        this.image = image;
        this.name = name;
        this.price = price;
        this.quantity = quantity;
        this.removed = removed;
    }

    @Override
    public String toString() {
        return "Order{" +
                "date='" + date + '\'' +
                ", docTypesName='" + docTypesName + '\'' +
                ", docId=" + docId +
                ", image='" + image + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", quantity=" + quantity +
                ", removed=" + removed +
                '}';
    }
}

class Test2 {
    public static void main(String[] args) {
        Order order = new Order("2017-10-16 12:07:07",
                "Приход",
                "564564867361367",
                "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
                "Молочный Изюм 100",
                102,
                45,
                0);

        Order order2 = new Order("2017-10-16 12:07:07",
                "Приход",
                "564564867361367",
                "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
                "Русская картошка чедар 50",
                46.3,
                45,
                0);

        Order order3 = new Order("2017-10-16 12:07:07",
                "Расход",
                "564564867361367",
                "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
                "Русская картошка чедар 50",
                46.3,
                45,
                0);

        Order order4 = new Order("2017-11-29 17:26:57",
                "Расход",
                "564564867361365",
                "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
                "Молочный Изюм 100",
                102,
                6,
                0);

        List<Order> orderList = new ArrayList<>();

        orderList.add(order);
        orderList.add(order2);
        orderList.add(order3);
        orderList.add(order4);

        System.out.println(orderList);
    }
}


Its JSON representation is:
[
    {
        "date": "2017-10-16 12:07:07",
        "docTypesName": "Приход",
        "docId": 564564867361367,
        "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
        "name": "Молочный Изюм 100",
        "price": 102,
        "quantity": 45,
        "removed": 0
    },
    {
        "date": "2017-10-16 12:07:07",
        "docTypesName": "Приход",
        "docId": 564564867361367,
        "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
        "name": "Русская картошка чедар 50",
        "price": 46.3,
        "quantity": 45,
        "removed": 0
    },
    {
        "date": "2017-10-16 12:07:07",
        "docTypesName": "Расход",
        "docId": 564564867361367,
        "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
        "name": "Русская картошка чедар 50",
        "price": 46.3,
        "quantity": 45,
        "removed": 0
    },
    {
        "date": "2017-11-29 17:26:57",
        "docTypesName": "Расход",
        "docId": 564564867361365,
        "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
        "name": "Молочный Изюм 100",
        "price": 102,
        "quantity": 6,
        "removed": 0
    },
]


Structure to get (JSON representation):
[
    {
        "date": "2017-10-16",
        documents: [
            {
                date: "2017-10-16 12:07:07",
                docId: "564564867361367",
                docTypesName: "Приход",
                products: [
                    {
                        "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
                        "name": "Молочный Изюм 100",
                        "price": 102,
                        "quantity": 45,
                    },
                    {
                        "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
                        "name": "Русская картошка чедар 50",
                        "price": 46.3,
                        "quantity": 45,
                    }
                ]
            },
            {
                "date": "2017-10-16 12:07:07",
                "docId": 564564867361367,
                "docTypesName": 'Расход',
                "products": [
                    {
                        "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
                        "name": "Русская картошка чедар 50",
                        "price": 46.3,
                        "quantity": 45,
                    }
                ]
            }
        ]
    },
    {
        "date": "2017-11-29",
        "documents": [
            {
                "date": "2017-11-29 17:26:57",
                "docId": 564564867361365,
                "docTypesName": 'Расход',
                "products": [
                    {
                        "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
                        "name": "Молочный Изюм 100",
                        "price": 102,
                        "quantity": 6,
                    }
                ]
            }
        ]
    }
]


Can you please tell me how to make this conversion?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-05-01
Hasanly @azerphoenix

Good afternoon!
I understand that you want to read from one JSON to pojo (Order). And then convert it to JSON of another structure?
If so, then divide the task into 3 steps:
1) deserialize from JSON to pojo Order
2) convert (map) Order -> OrderDto (call it what you want)
3) serialize OrderDto to JSON.
For the first and third steps, use jackson || gson. For the second stage, you can use the ModelMapper library || Map Structure. Or you can just write a static method that takes an Order and then sets its values ​​to OrderDto and returns an OrderDto.
What remains is to create a pojo for OrderDto based on the JSON structure you want to end up with.
You can use the site to generate pojo based on json - https://www.jsonschema2pojo.org/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question