Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

What is the best way to validate JSON input in AEM?

Avatar

Level 2

Hi 

 

I am trying to validate JSON input in SlingHttpServletRequest in AEM, I tried to use JSON Schema and JSON Schema validator but it doesn't compile with AEM. Every time I use the JSON validator I got an error on the AEM page, all the function buttons are gone except the "Create" button.

HanL1_0-1674440642575.png

 

 

I found that the root cause is because I used this JSON schema validator:

 

 

<dependency>
    <groupId>com.github.fge</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>2.2.6</version>
</dependency>

 

 

It breaks the app and someone of the OSGi components fail to start. Not sure why.

Here's the code:

 

 

    private String jsonSchema ="{\\\"$schema\\\":\\\"https://json-schema.org/draft/2019-09/schema\\\",\\\"$id\\\":\\\"http://example.com/example.json\\\",\\\"type\\\":\\\"object\\\",\\\"default\\\":{},\\\"title\\\":\\\"RootSchema\\\",\\\"properties\\\":{\\\"startDate\\\":{\\\"type\\\":\\\"string\\\",\\\"default\\\":\\\"\\\",\\\"title\\\":\\\"ThestartDateSchema\\\",\\\"examples\\\":[\\\"2021-01-01\\\"],\\\"format\\\":\\\"date\\\"},\\\"endDate\\\":{\\\"type\\\":\\\"string\\\",\\\"default\\\":\\\"\\\",\\\"title\\\":\\\"TheendDateSchema\\\",\\\"examples\\\":[\\\"2021-12-27\\\"],\\\"format\\\":\\\"date\\\"},\\\"governingBodyId\\\":{\\\"type\\\":\\\"string\\\",\\\"default\\\":\\\"\\\",\\\"title\\\":\\\"ThegoverningBodyIdSchema\\\",\\\"examples\\\":[\\\"a6f7A0000001B08QAE\\\"],\\\"pattern\\\":\\\"^[a-zA-Z0-9]{0,256}$\\\"},\\\"sourceId\\\":{\\\"type\\\":\\\"string\\\",\\\"default\\\":\\\"\\\",\\\"title\\\":\\\"ThesourceIdSchema\\\",\\\"examples\\\":[\\\"\\\"],\\\"pattern\\\":\\\"^[a-zA-Z0-9]{0,256}$\\\"},\\\"courseId\\\":{\\\"type\\\":\\\"string\\\",\\\"default\\\":\\\"\\\",\\\"title\\\":\\\"ThecourseIdSchema\\\",\\\"examples\\\":[\\\"\\\"],\\\"pattern\\\":\\\"^[a-zA-Z0-9]{0,256}$\\\"},\\\"useCompletionDate\\\":{\\\"type\\\":\\\"boolean\\\",\\\"default\\\":false,\\\"title\\\":\\\"TheuseCompletionDateSchema\\\",\\\"examples\\\":[true]}},\\\"examples\\\":[{\\\"startDate\\\":\\\"2021-01-01\\\",\\\"endDate\\\":\\\"2021-12-27\\\",\\\"governingBodyId\\\":\\\"a6f7A0000001B08QAE\\\",\\\"sourceId\\\":\\\"\\\",\\\"courseId\\\":\\\"\\\",\\\"useCompletionDate\\\":true}]}";
        String JsonData = IOUtils.toString(request.getInputStream(),"UTF-8");

        JsonNode dataNode = JsonLoader.fromString(JsonData);
        JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
        JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
        ProcessingReport processingReport = schema.validate(dataNode);
        if(!processingReport.isSuccess()){
        	log.error("Input JSON parameters don't match with the required pattern");
        	return;
        }

 

I also tried a different JSON schema validator library but still the same error.

If AEM doesn't compile with the JSON schema validator, what methods should I use to validate the JSON input (such as format, length etc.)?

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 2

Can I use it this way? 

    private boolean isJson(String str) {
    	try {
    		JsonParser parser = new JsonParser();
    		parser.parse(str);
    		return true;
    	}catch(Exception e) {
    		return false;
    	}
    }

If an exception been thrown out, means the input JSON data is wrong, otherwise it's correct.

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @HanL1 

Have you verified after deploying the code, whether your bundles are active or not? Please validate the /system/console/bundles.

If bundles are not active and the added jar is not getting resolved that means the given dependency is not available as a bundle in the target AEM.

You can follow the solution to this question if it is the same issue https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aws-sdk-dependencies-are-n...

Regards,

Arpit Varshney

Avatar

Level 2

I tried the method in that link but still the same error

Avatar

Community Advisor

AEM provide inbuild JSON api for json parsing which perform validations as well.

 com.google.gson.JsonParser

Avatar

Correct answer by
Level 2

Can I use it this way? 

    private boolean isJson(String str) {
    	try {
    		JsonParser parser = new JsonParser();
    		parser.parse(str);
    		return true;
    	}catch(Exception e) {
    		return false;
    	}
    }

If an exception been thrown out, means the input JSON data is wrong, otherwise it's correct.