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

issue with html data returned as json

Avatar

Level 4

I have this html data -

<img width="138px" height="74px" rev="om" src="//www.xyz.net/abc/2/3/4/data/image/data_6812?wid=138&amp;hei=74&amp;qlt=90,0&amp;resMode=sharp&amp;op_usm=1.1,0.5,1,0" alt="category"/><p>Category</p>

but when I call and get the json it not able to traslate the html properly and spits the output as -

{"name":"<img width="\" height="138px\\" rev="74px\\" src="om\\" alt="//www.xyz.net/abc/2/3/4/data/image/data_6812"/><p>Category<\/p>"}

 

instead it should have been like if it was correctly escaped- 

<img width=\"138px\" height=\"74px\" rev=\"om\" src=\"//www.xyz.net/abc/2/3/4/data/image/data_6812\" alt=\"Category\"/><p>Category</p>;

in my servlet I have set the content type as - application/json and the when I print the out put in my log file it is correctly escaping and showing the right value but in the browser it does not.

I have disabled the link checker, but still it does not load correctly. Does anyone know what else I need to do in order to have html data correctly being displayed on the browser? I could use the StringEscapeUtils.escapeHtml method but it escapes the whole html which is not desired.

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 4

MC Stuff wrote...

Hi Vsharm,

  Can you try with disabling xss filter?

Thanks,

 

I tried disabling the xss filters but that did not help.

The problem got isolated in "Day CQ Path Rewriting Transformer Factory"  from /system/console/configMgr where I have to keep the image mapping as black instead of "img:src". check the screen shot. after that we had to restart the Apache Sling Rewriterorg.apache.sling.rewriter from /system/console/bundles. It was a big relief for my project.

My problem has been solved. Hope this solution will help someone in this forum who gets into this issue.

Thank you everyone for your valuable input in figuring out and solving the issue.

View solution in original post

8 Replies

Avatar

Level 4

Hi, Vivek.

Sorry, but I couldn't follow you. What is this HTML data? Is it a property of a JCR node? Is it a cq page?
How has it been filled in? How is it being rendering? Are you using a JSP or a HTL as render script?

Can you please provide more details?

Regards,

Daniel.

Avatar

Level 10

Are you using custom Java logic to convert the HTML to JSON? 

Avatar

Level 4

Hello Daniel, Scott -  Thanks. Yes, it is a node property under jcr:content where I am storing this html  image data from a UI and from an API call from another system. I have  a servlet which is reading the data from the jcr node and exposing the contents in JSONObject using response.getWriter().print(jsonObj);. All other node property where there is no html are getting served properly in the json feed except this html.

To troubleshoot, I tried to expose this data using this servlet like below and same issue.

response.setHeader("Content-Type", "application/json");
        JSONObject jsonObj = new JSONObject();
      
        try {
            jsonObj.put("name", "<img width=\"138px\" height=\"74px\" rev=\"om\" src=\"//www.xyz.net/abc/2/3/4/data/image/data_6812\" alt=\"Category\"/><p>Category</p>");
            response.getWriter().print(jsonObj);
            } catch (IOException e) {
            log.error("IO Exception ", e);

Avatar

Level 4

daniel_henrique wrote...

Hi, Vivek.

Sorry, but I couldn't follow you. What is this HTML data? Is it a property of a JCR node? Is it a cq page?
How has it been filled in? How is it being rendering? Are you using a JSP or a HTL as render script?

Can you please provide more details?

Regards,

Daniel.

 

It is a node under jcr:content. attached screen shot.

Avatar

Level 9

Hi Vsharm,

if you are using jdk JSONObject then need to manually escape double quote OR use single quote instead of double. The other alternative is to use gson library which provides option to disable Html Escaping.

Thanks,

Avatar

Level 4

Hello all, thanks you for all of your responses. I have tried the GSON library as well as well as replacing the double quote and single quotes and same issue :(. Found the issue with the "<img" tag which is/was causing all this issue with making the json invalid.

I am not sure why would it do that. If I replace the "<img" with "&lt;img" then everything works just fine. I am not sure what's going on. Initially we thought it could be sling rewrite issue but at /libs/cq/config/rewriter/default, it has the content type as "html/text" and even my servlet has content type as "application/json". We tried disabling the sling rewrite but that too did not help. We had also tried disabling the link checker and that also did not work.

As a work around I am replacing "<img" with &lt;img. Please let me know if someone know why would it do that and if there is a work around.

Avatar

Level 9

Hi Vsharm,

  Can you try with disabling xss filter?

Thanks,

Avatar

Correct answer by
Level 4

MC Stuff wrote...

Hi Vsharm,

  Can you try with disabling xss filter?

Thanks,

 

I tried disabling the xss filters but that did not help.

The problem got isolated in "Day CQ Path Rewriting Transformer Factory"  from /system/console/configMgr where I have to keep the image mapping as black instead of "img:src". check the screen shot. after that we had to restart the Apache Sling Rewriterorg.apache.sling.rewriter from /system/console/bundles. It was a big relief for my project.

My problem has been solved. Hope this solution will help someone in this forum who gets into this issue.

Thank you everyone for your valuable input in figuring out and solving the issue.