AEM as Content Service - Sling Model Jackson Exporter - How to remove Unicode characters for RTE in JSON output | Community
Skip to main content
Level 2
July 29, 2022
Solved

AEM as Content Service - Sling Model Jackson Exporter - How to remove Unicode characters for RTE in JSON output

  • July 29, 2022
  • 1 reply
  • 1148 views

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-sling-model-exporter.html?lang=en#:~:text=Sling%20Model%20Exporter%20was%20introduced,different%20format%20such%20as%20JSON

 

Jackson Exporters Docx https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by DEBAL_DAS

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 -

 

 

 

1 reply

DEBAL_DAS
DEBAL_DASAccepted solution
New Member
July 30, 2022

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 -

 

 

 

Debal Das, Senior AEM Consultant