Hi Team,
My site is headless CMS (export AEM component dialog data as JSON), We use Jackson Exporter in Sling Models to export data as JSON.
Problem here is, We have RTE fields across our site. In JSON output for RTE field contains unicode characters for special symbols like <, >, space and etc.,. please find below.
"TestField" : "\u003Cp\u003E\u003Cb\u003E some test data in rte \u003C/b\u003E\u003C/p\u003E",
Some online JSON viewers sites and postman is processing the (removing escape chars) below data, but my UI team wants to remove these escape characters.
I have tried using @JsonRawValue annotation, which removes unicode characters and produces original data which added in RTE, but the problem with @JsonRawValue is it removes double quotes (") as well for the JSON value, which causes the json invalid like this
"TestField" : <p><b>some test data in rte</b></p>,
I want to keep double quotes but needs to removes unicode characters, Please suggest solutions for my issue
Sling Model Exporter Docx https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/develop-slin...
Jackson Exporters Docx https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
Solved! Go to Solution.
Views
Replies
Total Likes
Could you please try with the below code -
/** * */ package com.aem.demo.core.models; import org.apache.commons.text.StringEscapeUtils; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Exporter; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue; import com.fasterxml.jackson.annotation.JsonRawValue; /** * @author debal * */ @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = { "weretail/components/content/text" }) @Exporter(name = "jackson" , extensions = "json") public class RickTextModel { @ValueMapValue private String text; @JsonRawValue public String getText() { return StringEscapeUtils.unescapeHtml4(text).replaceAll("\\<.*?\\>", ""); } }
Final output -
I have added below maven dependency -
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
Persisted text at AEM repository via RTE -
Could you please try with the below code -
/** * */ package com.aem.demo.core.models; import org.apache.commons.text.StringEscapeUtils; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Exporter; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue; import com.fasterxml.jackson.annotation.JsonRawValue; /** * @author debal * */ @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = { "weretail/components/content/text" }) @Exporter(name = "jackson" , extensions = "json") public class RickTextModel { @ValueMapValue private String text; @JsonRawValue public String getText() { return StringEscapeUtils.unescapeHtml4(text).replaceAll("\\<.*?\\>", ""); } }
Final output -
I have added below maven dependency -
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
Persisted text at AEM repository via RTE -
Views
Like
Replies