import org.json.JSONObject;
public class JsonValidator {
/**
* Validates a JSON response against a set of constraints.
*
* @param jsonResponse The JSON response to validate.
* @param requiredFields A map of field names to expected data types (e.g., {"name": String, "age": Integer}).
* @param allowedValues A map of field names to allowed values (e.g., {"status": ["active", "inactive"]}).
* @return True if the JSON response meets all constraints, false otherwise.
*/
public static boolean validateJson(JSONObject jsonResponse, java.util.Map<String, String> requiredFields, java.util.Map<String, Object[]> allowedValues) {
if (jsonResponse == null) {
System.err.println("JSON response is null.");
return false;
}
// Check required fields
for (Map.Entry<String, String> entry : requiredFields.entrySet()) {
String fieldName = entry.getKey();
String expectedType = entry.getValue();
if (!jsonResponse.has(fieldName)) {
System.err.println("Missing required field: " + fieldName);
return false;
}
Object value = jsonResponse.get(fieldName);
if (expectedType.equals("String") && !(value instanceof String)) {
System.err.println("Field " + fieldName + " should be a String.");
return false;
} else if (expectedType.equals("Integer") && !(value instanceof Integer)) {
System.err.println("Field " + fieldName + " should be an Integer.");
return false;
} else if (expectedType.equals("Double") && !(value instanceof Double)) {
System.err.println("Field " + fieldName + " should be a Double.");
return false;
} else if (expectedType.equals("Boolean") && !(value instanceof Boolean)) {
System.err.println("Field " + fieldName + " should be a Boolean.");
return false;
}
}
// Check allowed values
for (Map.Entry<String, Object[]> entry : allowedValues.entrySet()) {
String fieldName = entry.getKey();
Object[] allowed = entry.getValue();
if (jsonResponse.has(fieldName)) {
Object value = jsonResponse.get(fieldName);
boolean isValid = false;
if (allowed != null) {
for (Object allowedValue : allowed) {
if (value.equals(allowedValue)) {
isValid = true;
break;
}
}
}
if (!isValid) {
System.err.println("Field " + fieldName + " has an invalid value: " + value);
return false;
}
}
}
return true; // All constraints met
}
public static void main(String[] args) {
// Example usage
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("name", "John Doe");
jsonResponse.put("age", 30);
jsonResponse.put("status", "active");
java.util.Map<String, String> requiredFields = new java.util.HashMap<>();
requiredFields.put("name", "String");
requiredFields.put("age", "Integer");
java.util.Map<String, Object[]> allowedValues = new java.util.HashMap<>();
Object[] allowedStatuses = {"active", "inactive"};
allowedValues.put("status", allowedStatuses);
boolean isValid = validateJson(jsonResponse, requiredFields, allowedValues);
System.out.println("JSON is valid: " + isValid);
//Example of invalid JSON
JSONObject invalidJson = new JSONObject();
invalidJson.put("name", 123);
invalidJson.put("age", "abc");
isValid = validateJson(invalidJson, requiredFields, allowedValues);
System.out.println("Invalid JSON is valid: " + isValid);
}
}
Add your comment