+ Start a Discussion
toufik_attoufik_at 

JSON Schema validation in APEX

Hi, 
I wonder if there is in APEX a way to validate a JSON string against a defined json schema ?
Somthing like  :
If(isValid(jsonString, jsonSchema)) {
doCallout();
}
Basically, we are unable to make callouts to a third party system because they want us to validate our JSON payload with a schema before sending as part of their quality requirements.

Link to the schema  : 
https://realtime-listings.webservices.zpg.co.uk/docs/v1.0/schemas/listing/update.json
Ashish DevAshish Dev
I think your best bet would be to create json serialize it, and then deserialize it agains your custom schema ApexType.

 
KeshabKeshab
I am using this techniqueue across all rest callout , you can use the same. 

public boolean isValid(String JsoneString){
    try{
    jsonSchema jsonObj=new jsonSchema();
        jsonObj=(jsonSchema)JSON.deserialize(JsoneString,jsonSchema);
        return true;
    }catch(Exception e){
    
        return false;
    }
}
public class jsonSchema{
    String Name;
    String Age;
}