1. import org.json.JSONObject;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class JsonValidator {
  5. public static void validateJson(String jsonString) {
  6. try {
  7. JSONObject jsonObject = new JSONObject(jsonString);
  8. // Define default values for expected fields
  9. Map<String, Object> defaults = new HashMap<>();
  10. defaults.put("name", "Unknown");
  11. defaults.put("age", 0);
  12. defaults.put("city", "DefaultCity");
  13. defaults.put("isActive", false);
  14. // Validate required fields
  15. if (jsonObject.has("name") && jsonObject.get("name") == null) {
  16. System.err.println("Error: 'name' field is missing.");
  17. jsonObject.put("name", defaults.get("name"));
  18. } else if (jsonObject.has("name") && ((String) jsonObject.get("name")).isEmpty()) {
  19. System.err.println("Error: 'name' field is empty.");
  20. jsonObject.put("name", defaults.get("name"));
  21. }
  22. if (jsonObject.has("age") && jsonObject.get("age") == null) {
  23. System.err.println("Error: 'age' field is missing.");
  24. jsonObject.put("age", defaults.get("age"));
  25. } else if (jsonObject.has("age") && !jsonObject.get("age").isNumeric()) {
  26. System.err.println("Error: 'age' field is not a number.");
  27. jsonObject.put("age", defaults.get("age"));
  28. }
  29. if (jsonObject.has("city") && jsonObject.get("city") == null) {
  30. System.err.println("Error: 'city' field is missing.");
  31. jsonObject.put("city", defaults.get("city"));
  32. }
  33. if (jsonObject.has("isActive") && jsonObject.get("isActive") == null) {
  34. System.err.println("Error: 'isActive' field is missing.");
  35. jsonObject.put("isActive", defaults.get("isActive"));
  36. } else if (jsonObject.has("isActive") && !jsonObject.get("isActive").isBoolean()) {
  37. System.err.println("Error: 'isActive' field is not a boolean.");
  38. jsonObject.put("isActive", defaults.get("isActive"));
  39. }
  40. // Print the validated JSON (optional)
  41. System.out.println("Validated JSON: " + jsonObject.toString(2));
  42. } catch (Exception e) {
  43. System.err.println("Error parsing JSON: " + e.getMessage());
  44. System.err.println("Stacktrace: " + e.printStackTrace());
  45. }
  46. }
  47. public static void main(String[] args) {
  48. // Example usage
  49. String json1 = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\", \"isActive\": true}";
  50. String json2 = "{\"age\": 25, \"city\": \"London\"}";
  51. String json3 = "{\"name\": null, \"age\": \"abc\"}";
  52. String json4 = "{\"name\": \"\", \"age\": 25}";
  53. String json5 = "{\"name\": \"Jane\", \"age\": 28, \"city\": null, \"isActive\": \"yes\"}";
  54. System.out.println("Validating JSON 1:");
  55. validateJson(json1);
  56. System.out.println();
  57. System.out.println("Validating JSON 2:");
  58. validateJson(json2);
  59. System.out.println();
  60. System.out.println("Validating JSON 3:");
  61. validateJson(json3);
  62. System.out.println();
  63. System.out.println("Validating JSON 4:");
  64. validateJson(json4);
  65. System.out.println();
  66. System.out.println("Validating JSON 5:");
  67. validateJson(json5);
  68. System.out.println();
  69. }
  70. }

Add your comment