I have successfully implemented a Sling model exporter, but I'm facing an issue where the entire dataset, including unwanted information like null values, is being exported in JSON. How can I ensure that only the relevant data, excluding the unwanted details, is shared through the JSON exporter in AEM? Are there any specific annotation I can apply to achieve this selective data export or is it possible at this level of implementation?
This is how my output looks like
{"Hello":{"location":"pune","name":"john","surname":"h",”phonenumber”:”null”}}
In the above output Phone number data has null .so,I don’t want to export that can you please help on this issue
Yes, it’s possible to achieve using @JSONIgnore
. Here's a sample code snippet
@Exporter(
name = "jackson", extensions = "json", selector = "geeks",
)
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "wknd/components/hello")
public class Hello {
@ValueMapValue
private String imagesrc;
public String getImagesrc() {
return null;
}
@ValueMapValue
private String imagetitle;
@JsonIgnore
public String getImagetitle() {
return imagetitle;
}
}
{"imagetitle":"kotlin"}
This annotation is used to indicate that a specific field or method should be ignored during the JSON serialization and deserialization process. It helps exclude certain properties from being included in the JSON output.
In the above example, imagesrc
is null so it is not exporting in JSON format.
If you want to learn about JSON annotations go through it here