1. import org.json.JSONObject;
  2. import org.json.JSONArray;
  3. public class ResponseTransformer {
  4. /**
  5. * Transforms a JSON response to a specific format.
  6. * @param jsonString The JSON string to transform.
  7. * @param transformationType The type of transformation to perform (e.g., "user", "product").
  8. * @return A transformed JSON string, or null if transformation fails.
  9. */
  10. public static String transformResponse(String jsonString, String transformationType) {
  11. try {
  12. JSONObject json = new JSONObject(jsonString);
  13. if (transformationType.equals("user")) {
  14. // Example: Transform to a user-specific format
  15. JSONObject transformedJson = new JSONObject();
  16. transformedJson.put("userId", json.getString("id"));
  17. transformedJson.put("userName", json.getString("name"));
  18. transformedJson.put("userEmail", json.getString("email"));
  19. return transformedJson.toString();
  20. } else if (transformationType.equals("product")) {
  21. // Example: Transform to a product-specific format
  22. JSONObject transformedJson = new JSONObject();
  23. transformedJson.put("productId", json.getString("product_id"));
  24. transformedJson.put("productName", json.getString("product_name"));
  25. transformedJson.put("productPrice", Double.parseDouble(json.getString("price"))); // Parse price as double
  26. return transformedJson.toString();
  27. } else {
  28. // Unknown transformation type
  29. return null;
  30. }
  31. } catch (Exception e) {
  32. // Handle potential JSON parsing errors
  33. System.err.println("Error transforming response: " + e.getMessage());
  34. return null;
  35. }
  36. }
  37. /**
  38. * Transforms a JSON response with array of data.
  39. * @param jsonString The JSON string to transform.
  40. * @param transformationType The type of transformation to perform (e.g., "user", "product").
  41. * @return A transformed JSON string, or null if transformation fails.
  42. */
  43. public static String transformResponseArray(String jsonString, String transformationType) {
  44. try {
  45. JSONArray jsonArray = new JSONArray(jsonString);
  46. JSONObject transformedArray = new JSONObject();
  47. JSONArray transformedArrayData = new JSONArray();
  48. for (int i = 0; i < jsonArray.length(); i++) {
  49. JSONObject item = jsonArray.getJSONObject(i);
  50. if (transformationType.equals("user")) {
  51. JSONObject transformedItem = new JSONObject();
  52. transformedItem.put("userId", item.getString("id"));
  53. transformedItem.put("userName", item.getString("name"));
  54. transformedItem.put("userEmail", item.getString("email"));
  55. transformedArrayData.put(transformedItem);
  56. } else if (transformationType.equals("product")) {
  57. JSONObject transformedItem = new JSONObject();
  58. transformedItem.put("productId", item.getString("product_id"));
  59. transformedItem.put("productName", item.getString("product_name"));
  60. transformedItem.put("productPrice", Double.parseDouble(item.getString("price")));
  61. transformedArrayData.put(transformedItem);
  62. }
  63. else {
  64. return null;
  65. }
  66. }
  67. transformedArray.put(transformedArrayData);
  68. return transformedArray.toString();
  69. } catch (Exception e) {
  70. System.err.println("Error transforming response: " + e.getMessage());
  71. return null;
  72. }
  73. }
  74. public static void main(String[] args) {
  75. // Example Usage
  76. String userJson = "{\"id\": 123, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}";
  77. String transformedUserJson = transformResponse(userJson, "user");
  78. System.out.println("Transformed User: " + transformedUserJson);
  79. String productJson = "{\"product_id\": \"456\", \"product_name\": \"Awesome Gadget\", \"price\": \"99.99\"}";
  80. String transformedProductJson = transformResponse(productJson, "product");
  81. System.out.println("Transformed Product: " + transformedProductJson);
  82. String userJsonArray = "[{\"id\": 123, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}, {\"id\": 456, \"name\": \"Jane Smith\", \"email\": \"jane.smith@example.

Add your comment