0

I have JSON response like this:

{
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "collection": [
        {
            "col_1_1": "string",
            "col_1_2": 37,
            "col_1_3": "string",
            "col_1_4": "string",    
        },
        {
            "col_2_1": "string",
            "col_2_2": 37,
            "col_2_3": "string",
            "col_2_4": "string",
          
        }
    ]
}

How can I achieve result like this:

{
    {
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "col_1_1": "string",
    "col_1_2": 37,
    "col_1_3": "string",
    "col_1_4": "string",    
    }, 
    {
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "col_2_1": "string",
    "col_2_2": 37,
    "col_2_3": "string",
    "col_2_4": "string", 
    }               
}

It is possible to make it without external libraries ? How can I achieve it ? Has anyone ever dealt with such an issue?

POJOS:

public class MainDTO {
    private String param1;
    private String param2;
    private String param3;
    private String param4;
    private Set<CollectionDTO> collection;
}


class CollectionDTO {
    private String param1;
    private String param2;
    private String param3;
    private String param4;
}

Class is something like that. I have tried in many ways to no avail. Generally, the mapper's final method should only take one parameter in the form MainDTO

maptoDto(MainDTO dto) {...}

I made a mapping method that takes MainDTO and CollectionDTO, but further inside the method I think I need to do another mapping as well so that each element is extracted: /

mat373
  • 171
  • 4
  • 13
  • 1
    What did you try so far? What do your pojos look like? You'd basically need to parse the incoming json and then build your result objects using each of the collection elements individually and combining it with the outer object. Also, your result json looks wrong, the outer portion should be an array and not an object. – Thomas Sep 09 '21 at 09:44
  • What's your problem, set up a spring boot project? define a data model matching your request? transform the former data model to match with the later? – josejuan Sep 09 '21 at 09:45
  • I updated the entry – mat373 Sep 09 '21 at 09:51

2 Answers2

0

Your Json is invalid because you need to have an array instead of the object:

{
    "first" :{ // You need to name this object
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "col_1_1": "string",
    "col_1_2": 37,
    "col_1_3": "string",
    "col_1_4": "string",    
    }, 
    "second": {
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "col_2_1": "string",
    "col_2_2": 37,
    "col_2_3": "string",
    "col_2_4": "string", 
    }               
}

Otherwise You can do an array as follows:

[
    { 
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "col_1_1": "string",
    "col_1_2": 37,
    "col_1_3": "string",
    "col_1_4": "string",    
    }, 
    {
    "1": "string",
    "2": "string",
    "3": "string",
    "4": "string",
    "col_2_1": "string",
    "col_2_2": 37,
    "col_2_3": "string",
    "col_2_4": "string", 
    }               
]

To achieve this you need to create a third modal than compute each one to transfer it to the second modal.

public class Achieve {
    private Set<AchieveElement> collection;
}

public class AchieveElement {
    private String param1;
    private String param2;
    private String param3;
    private String param4;    
    @JsonProperty("1")
    private String parameter1;
    @JsonProperty("2")
    private String parameter2;
    @JsonProperty("3")
    private String parameter3;
    @JsonProperty("4")
    private String parameter4;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

OK i have solution. I have created one DTO with parameters that are needed, which are in both entities, the main entity and the entity that is the collection. Then the method that creates the dto object, where it gives both classes in parameters:

private fun createDTO(dto: MainEntity, collection: CollectionEntity): MyDTO =
    MyDto(
       //...parameter assignment
    )

and mapping function (kotlin):

internal fun toDTO(dto: MainEntity): Set<MyDTO > {
    return dto.collestion
        .map { createDTO(dto, it) }
        .toSet()
}

It's working ;) Thanks for sugestions

mat373
  • 171
  • 4
  • 13