'How to combine JSON values in the response using Java

I am currently working on a school project. We have a series of response templates in JSON format that will take values from the request and then return it accordingly in the response when run in postman.

e.g Request: { "Application_id":123456 }

Response: { "Application_id: 123456, TIMESTAMP: 20220501}

I am able to get these values in the response but the issue I am running accross now is figuring out how to combine 2 values in the request into one like so:

Request: { "Application_id":123456 "user_id_first_six": 456789 "user_id_last_four": 1234 }

Expected Response: { "Application_id: 123456, TIMESTAMP: 20220501, combined_id:456789****1234}

what I have tried is to put combined_id : "user_id_first_six"+******+"user_id_last_four" but this doesnt work.

Apologies if I cant be more specific as there are portions that I have left out due to confidentiality issues.



Solution 1:[1]

The easiest way to achieve this in Java would be to use JSONObject. In your Request-Handler, add two parameters of Type JSONObject and then merge them:

jsonObj.putAll(jsonObj1)

Solution 2:[2]

Thanks all for the guidance. I basically did what Knu8 suggested and extracted the values using Matcher+Regex (<(.*)>)(\W*)(<(.*)>) and converted them to strings and then used StringBuilder to append all the components together.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominik Lovetinsky
Solution 2 Benjamin Chan