• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Retrieving a response from a third party API and convert it to a new format [Spring Boot and Java]

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a task whereby I need to integrate with a third party API. I am planning to do something along these lines: https://spring.io/guides/gs/consuming-rest/, meaning using Spring’s RestTemplate to retrieve the response of an external API from within my Spring Boot application. Until this all good, but the second step is to "re-construct" aka convert the existing response to a new format.

For example, the original response from the 3rd party API is something like:




and the expected customised output should look like:



Expected changes are both in format/data structure (e.g. field_3_1 is now a field inside an array) and the field naming convention (e.g: "id" becomes "application_id"). Is there any Spring library/class I can use for easily make the conversion? I will create a new model class with the customised response, but I am not sure about the approach for re-structuring the data. Can you help? Is there any example I can look it up?

Thank you,

G.

 
Bartender
Posts: 2124
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may need to use a Json parser such as JSONParser to parse your original Json object and collect the data.
Then, use your data to create a new Json object.


Reference:
https://www.geeksforgeeks.org/parse-json-java/
 
Geane Norm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:You may need to use a Json parser such as JSONParser to parse your original Json object and collect the data.
Then, use your data to create a new Json object.


Reference:
https://www.geeksforgeeks.org/parse-json-java/



Hi,
thank you for your reply. So, as it stands, I can successfully consume the third party service for which I created a Dto with all expected @JsonProperty per field. I have also created an entity for the expected new format and its Dto. What is the best to go about? Can I convert the initial Dto with the new format one and in my Controller I will return the new format? Would you happen to have any example I can check?

Thank you,

 
Himai Minh
Bartender
Posts: 2124
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You may need to create a DTO to transform the object. And the return type is that DTO.
reply
    Bookmark Topic Watch Topic
  • New Topic