Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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 -

 

DEBAL_DAS_0-1659203089433.png

 

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_1-1659203173048.png

 

 

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

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 -

 

DEBAL_DAS_0-1659203089433.png

 

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_1-1659203173048.png