import org.json.JSONObject;
import org.json.JSONArray;
public class ResponseTransformer {
/**
* Transforms a JSON response to a specific format.
* @param jsonString The JSON string to transform.
* @param transformationType The type of transformation to perform (e.g., "user", "product").
* @return A transformed JSON string, or null if transformation fails.
*/
public static String transformResponse(String jsonString, String transformationType) {
try {
JSONObject json = new JSONObject(jsonString);
if (transformationType.equals("user")) {
// Example: Transform to a user-specific format
JSONObject transformedJson = new JSONObject();
transformedJson.put("userId", json.getString("id"));
transformedJson.put("userName", json.getString("name"));
transformedJson.put("userEmail", json.getString("email"));
return transformedJson.toString();
} else if (transformationType.equals("product")) {
// Example: Transform to a product-specific format
JSONObject transformedJson = new JSONObject();
transformedJson.put("productId", json.getString("product_id"));
transformedJson.put("productName", json.getString("product_name"));
transformedJson.put("productPrice", Double.parseDouble(json.getString("price"))); // Parse price as double
return transformedJson.toString();
} else {
// Unknown transformation type
return null;
}
} catch (Exception e) {
// Handle potential JSON parsing errors
System.err.println("Error transforming response: " + e.getMessage());
return null;
}
}
/**
* Transforms a JSON response with array of data.
* @param jsonString The JSON string to transform.
* @param transformationType The type of transformation to perform (e.g., "user", "product").
* @return A transformed JSON string, or null if transformation fails.
*/
public static String transformResponseArray(String jsonString, String transformationType) {
try {
JSONArray jsonArray = new JSONArray(jsonString);
JSONObject transformedArray = new JSONObject();
JSONArray transformedArrayData = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i);
if (transformationType.equals("user")) {
JSONObject transformedItem = new JSONObject();
transformedItem.put("userId", item.getString("id"));
transformedItem.put("userName", item.getString("name"));
transformedItem.put("userEmail", item.getString("email"));
transformedArrayData.put(transformedItem);
} else if (transformationType.equals("product")) {
JSONObject transformedItem = new JSONObject();
transformedItem.put("productId", item.getString("product_id"));
transformedItem.put("productName", item.getString("product_name"));
transformedItem.put("productPrice", Double.parseDouble(item.getString("price")));
transformedArrayData.put(transformedItem);
}
else {
return null;
}
}
transformedArray.put(transformedArrayData);
return transformedArray.toString();
} catch (Exception e) {
System.err.println("Error transforming response: " + e.getMessage());
return null;
}
}
public static void main(String[] args) {
// Example Usage
String userJson = "{\"id\": 123, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}";
String transformedUserJson = transformResponse(userJson, "user");
System.out.println("Transformed User: " + transformedUserJson);
String productJson = "{\"product_id\": \"456\", \"product_name\": \"Awesome Gadget\", \"price\": \"99.99\"}";
String transformedProductJson = transformResponse(productJson, "product");
System.out.println("Transformed Product: " + transformedProductJson);
String userJsonArray = "[{\"id\": 123, \"name\": \"John Doe\", \"email\": \"john.doe@example.com\"}, {\"id\": 456, \"name\": \"Jane Smith\", \"email\": \"jane.smith@example.
Add your comment