Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Aem sling Rewriter for json values

Avatar

Community Advisor

Hi, 

To meet our requirements I have implemented a filter in aem for certain path and is returning the json response from filter. Earlier I was facing issue with link rewriter removing the href invalid valid link from json even after disabling the disable link checking from aem link checker transformer OSGI configs.

To fix this I wrote a custom rewriter xml with custom generator and htmlparser generator and htmlwriter serializer. However in my custom transformer class whenever I am reading the href value for below [1] string it getting href as “\”. I believe it’s because we are using backslash to use quotes for href to get easily parsed on html. My html on json looks like

[1] {“message”: “this is text having href and aria-label <a href=\”/en/mypage.html \“ target=\”_blank\” aria-label=\”test\””}

Does any one has faced similar issue and was able to fix it?

NOTE: above html can’t be changed and used to remain as it is.

Not able to find any Apache documentation, what all different generators are allowed, I believe the reason is generator is converting the json strain in xml and as href starting value is \ so it’s read the href value as empty in sax parser

4 Replies

Avatar

Community Advisor

Java typically appends a backslash to escape the double-quote character, so when the text is output, it will contain the quotes within it.

 

It should not be a problem depending on the context where you are using this JSON, if I am not mistaken the links will still work correct?, that being said, I think the easiest solution in your case would be to construct your JSON object with a single quotation so you can avoid the issue. You will need to output something like this:

 

{'message': 'this is text having href and aria-label <a href=”/en/mypage.html“ target=”_blank” aria-label=”test”'}

You can explore something like this: https://fasterxml.github.io/jackson-core/javadoc/2.9/com/fasterxml/jackson/core/JsonParser.Feature.h...

objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);


Esteban Bustamante

Avatar

Community Advisor

Single quotation is not possible in my use case as the data being exposed to be consumed by third party and won’t look good and also the json agreed in standard format.

coming out to the object mapper I am using the JSONObject.toStrung() to convert to string as with objectmapper it adds extra iterable variable in json string rendered by filter response on the browser

Avatar

Administrator

To fix this issue, you can either:

  • Escape the backslash in the href link. You can do this by adding a backslash before the backslash in the href link. For example, the href link in your example would become \"/en/mypage.html\".
  • Use a custom link rewriter transformer. You can create a custom link rewriter transformer that is configured to handle backslashes in the href link. This would be a more complex solution, but it would give you the most control over how the href link is handled.


Kautuk Sahni

Avatar

Community Advisor

Hi Kautuk,

Yes the href is actually having backslash in the url like you mention 

\"/en/mypage.html\"

I had already created a new sling link rewriter transformer and while overriding the startElement(String uri, String localName, String qName, Attributes atts) throws SAXException{ atts.getValue(“href”) is giving me the value as “\” }

I believe that’s happening because first character in href is “\”. I am not sure if I need to overwrite other method on transformer. 
in my rewriter config I am using htmlparser as generatorType and htmlwriter as serializerType, even not sure if I need to use some other generators and serializer. Not able to find sufficient documentation on this