简体   繁体   中英

How do I merge a list of json response inside another list in spring boot or java?

UserDetails and Failures response are obtained by two seperate db calls. I want to insert the failures object inside userdetails object. The two arrays has to be associated based on the context. This is my current JSON response:-

"Userdetails": [
        {
            "context": "C",
            "count": 6,
            "avgResponseTime": 133.8333,
            "minResponseTime": 16.0,
            "maxResponseTime": 293.0,
            "success": 6
        },
        {
            "context": "B",
            "count": 1,
            "avgResponseTime": 44.0,
            "minResponseTime": 44.0,
            "maxResponseTime": 44.0,
            "success": 0
        },
        {
            "context": "A",
            "count": 101,
            "avgResponseTime": 68.7822,
            "minResponseTime": 9.0,
            "maxResponseTime": 404.0,
            "success": 96
        }
    ],

"failures": [
        {
            "statusCode": 0,
            "context": "A"
        },
        {

            "statusCode": 400,
            "context": "B"
        },
        {

            "statusCode": 404,
            "context": "C"
        }
    ]

Response that I need:-

"Userdetails": [
        {
            "context": "C",
            "count": 6,
            "avgResponseTime": 133.8333,
            "minResponseTime": 16.0,
            "maxResponseTime": 293.0,
            "success": 6,
            "failures": [
        {
            "statusCode": 0,
            "context": "A"
        }
    ]
        },

    ], and so on...

How do I insert the failures response bean inside the Userdetails bean as shown above. Any help would be appreciated.

My suggestion below

  • Convert the failures array and to a hashmap of type where the key is the context and the value is the corresponding failure object
  • Stream the UserDetails array and pick the corresponding value of context from the failures hashmap created

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM