1. import org.json.JSONArray;
  2. import org.json.JSONObject;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class JsonNormalizer {
  6. /**
  7. * Normalizes a JSON array of objects for synchronous execution.
  8. * Converts all objects to a consistent map structure.
  9. * @param jsonArray The JSON array to normalize.
  10. * @return A map where keys are consistent and values are extracted from the JSON objects.
  11. * Returns an empty map if the input is null or not a valid array.
  12. */
  13. public static Map<String, String> normalizeJsonArray(JSONArray jsonArray) {
  14. Map<String, String> normalizedData = new HashMap<>();
  15. if (jsonArray == null || jsonArray.isEmpty()) {
  16. return normalizedData; // Return empty map for null or empty array
  17. }
  18. for (int i = 0; i < jsonArray.length(); i++) {
  19. JSONObject jsonObject = jsonArray.getJSONObject(i);
  20. if (jsonObject != null) {
  21. // Extract data, handling potential null values gracefully
  22. String key1 = jsonObject.getString("id"); // Consistent key
  23. String key2 = jsonObject.getString("name"); // Consistent key
  24. String key3 = jsonObject.getString("value"); // Consistent key
  25. normalizedData.put(key1, key2 + "_" + key3); // Combine values, using consistent keys
  26. }
  27. }
  28. return normalizedData;
  29. }
  30. /**
  31. * Normalizes a single JSON object for synchronous execution.
  32. * Converts the object to a consistent map structure.
  33. * @param jsonObject The JSON object to normalize.
  34. * @return A map where keys are consistent and values are extracted from the JSON object.
  35. * Returns an empty map if the input is null or not a valid object.
  36. */
  37. public static Map<String, String> normalizeJsonObject(JSONObject jsonObject) {
  38. Map<String, String> normalizedData = new HashMap<>();
  39. if (jsonObject == null) {
  40. return normalizedData;
  41. }
  42. try {
  43. String key1 = jsonObject.getString("id");
  44. String key2 = jsonObject.getString("name");
  45. String key3 = jsonObject.getString("value");
  46. normalizedData.put(key1, key2 + "_" + key3);
  47. } catch (Exception e) {
  48. // Handle potential errors during JSON extraction
  49. System.err.println("Error normalizing JSON object: " + e.getMessage());
  50. return new HashMap<>(); //Return empty map on error
  51. }
  52. return normalizedData;
  53. }
  54. public static void main(String[] args) {
  55. // Example Usage:
  56. String jsonString = "[{\"id\": \"1\", \"name\": \"Item1\", \"value\": \"10\"}, {\"id\": \"2\", \"name\": \"Item2\", \"value\": \"20\"}]";
  57. JSONArray jsonArray = new JSONArray(jsonString);
  58. Map<String, String> normalizedArray = normalizeJsonArray(jsonArray);
  59. System.out.println("Normalized Array: " + normalizedArray);
  60. JSONObject jsonObject = new JSONObject("{\"id\": \"3\", \"name\": \"Item3\", \"value\": \"30\"}");
  61. Map<String, String> normalizedObject = normalizeJsonObject(jsonObject);
  62. System.out.println("Normalized Object: " + normalizedObject);
  63. }
  64. }

Add your comment