davids182009
davids182009

Reputation: 451

Create JSONObject dynamically Java

I have a project in SpringBoot and I am looking to create a JSON object to send to my FrontEnd project, I am looking to create a JSON like this:

{    "result": {    "data": [      {        "month": "January",        "APERTURAS": 0      },      {        "month": "February",        "APERTURAS": 0      },      {        "month": "March",        "APERTURAS": 0      },      {        "month": "April",        "APERTURAS": 0      },      {        "month": "May",        "APERTURAS": 0      },      {        "month": "June",        "APERTURAS": 0      },      {        "month": "July",        "APERTURAS": 0      },      {        "month": "August",        "APERTURAS": 3      },      {        "month": "September",        "APERTURAS": 11      },      {        "month": "October",        "APERTURAS": 3      },      {        "month": "November",        "APERTURAS": 0      },      {        "month": "December",        "APERTURAS": 0      }    ]    }    }

in this one, amounts are being totaled per month, I am making a query and with this I obtain the months in which there is information, I need to create the json with the information of all the months of the year, but if for example the query does not return the amount of a specific month or months, I want to indicate this in zero.

with my code I am getting the following JSON:

{  "result": {    "data": [      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {},      {        "APERTURAS": 0,        "month": "August"      },      {},      {},      {},      {        "APERTURAS": 0,        "month": "September"      },      {},      {},      {},      {        "APERTURAS": 3,        "month": "October"      },      {},      {},      {},      {},      {},      {}    ]  }}

This is my code:

private static final String[] MESES_COMPLETO = {"January", "February", "March", "April", "May", "June", "July", 
        "August", "September", "October", "November", "December"};

List<ReporteTipoActoMesInterfaceDTO> resultadoAperturas = repositorioReporte.reporteTipoActoAperturaMes(String.valueOf(year));

List<Object> reporteList = new ArrayList<Object>();     
JSONObject reporteJson = new JSONObject();

for (int j = 0; j < MESES_COMPLETO.length; j++) {

JSONObject reporteJson = new JSONObject();
            
for (int k = 0; k < resultadoAperturas.size(); k++) {
    JSONObject reporteJson2 = new JSONObject();
    String mes = resultadoAperturas.get(k).getmes().replaceAll("\\s+","");
                
    if (!MESES_COMPLETO[j].equals(mes)) {                       
        reporteJson.put("month", MESES_COMPLETO[j]);
        reporteJson.put("APERTURAS", 0);
        reporteList.add(reporteJson2);
    } 
    else {

        if (Arrays.asList(MESES_COMPLETO[j]).contains(mes)) {

            reporteJson.put("month", mes);
            reporteJson.put("APERTURAS", resultadoAperturas.get(k).getexpedientes());
            reporteList.add(reporteJson);
        }
    }
}
}       
        
JSONObject jsonData = new JSONObject();
jsonData.put("data", reporteList);
        
jsonResponse = new JSONObject().put("result", jsonData).toString();

This is DTO

public interface ReporteTipoActoMesInterfaceDTO {

Long getexpedientes();

String getmes();
}

This is the result of the query

EXPEDIENTES MES
3           August    
11          September
3           October

What am i doing wrong?

Upvotes: 0

Views: 155

Answers (1)

unconditional
unconditional

Reputation: 7656

In your case it looks like there's no real reason to do manual JSON construction via JSONObjects.

Just define your DTOs to match the desired JSON structure, fill it with your data and use Jackson to serialize it altogether.

DTOs:

    @Data
    class ResultDto {
        DataDto result;

        @Data
        static class DataDto {
            List<MonthDataDto> data;
        }

        @Data
        static class MonthDataDto {
            String month;
            @JsonProperty("APERTURAS")
            Integer aperturas;
        }
    }

Serialization:

ResultDto resultDto = ...
String jsonResponse = new ObjectMapper().writeValueAsString(resultDto);

Be sure to have com.fasterxml.jackson.core:jackson-databind dependency in your project.

Upvotes: 2

Related Questions