Expand my Community achievements bar.

SOLVED

Exclude or ignore certain properties from being serialized in the JSON output

Avatar

Level 2

I have a Sling Model exported as JSON using the @exporter annotation. But some properties (like internal IDs or sensitive info) should not appear in the JSON response. How can I exclude those fields from the exported JSON?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @NamitaDu,

To exclude or ignore properties in Sling Model Exporter JSON output, you can use the @JsonIgnore annotation from the Jackson library on the getter methods or fields you want to omit.

Official Reference:

See Adobe Experience Manager Sling Model Exporter docs for details:

 


Santosh Sai

AEM BlogsLinkedIn


View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @NamitaDu,

To exclude or ignore properties in Sling Model Exporter JSON output, you can use the @JsonIgnore annotation from the Jackson library on the getter methods or fields you want to omit.

Official Reference:

See Adobe Experience Manager Sling Model Exporter docs for details:

 


Santosh Sai

AEM BlogsLinkedIn


Avatar

Community Advisor

Hi @NamitaDu ,

You need to:

Use @JsonIgnore from the Jackson library

Annotate the getter method (or field) of the property you want to exclude from the JSON output


1. Maven Dependency (Optional for standalone testing)

Ensure Jackson annotations are available (AEM provides it by default in author run modes):

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.15.3</version> <!-- or your compatible version -->
</dependency>

2. Working Example: Sling Model

package com.myproject.core.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model(adaptables = Resource.class,
       adapters = {MyModel.class, ComponentExporter.class},
       resourceType = MyModel.RESOURCE_TYPE,
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
          extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class MyModel implements ComponentExporter {

    protected static final String RESOURCE_TYPE = "myproject/components/content/mymodel";

    @ValueMapValue
    private String title;

    @ValueMapValue
    private String description;

    @ValueMapValue
    private String internalId; // we want to exclude this

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    // This will NOT appear in JSON output
    @JsonIgnore
    public String getInternalId() {
        return internalId;
    }

    @Override
    public String getExportedType() {
        return RESOURCE_TYPE;
    }
}

 

3. Resulting JSON Output

Assuming resource has all 3 properties set:

{
  "title": "Hello AEM",
  "description": "Some visible text",
  ":type": "myproject/components/content/mymodel"
}

internalId is excluded from JSON.

Note:

If you need dynamic filtering (e.g., show/hide fields based on user roles or endpoints), consider:

Using @JsonView annotations and configuring custom views

Creating a custom DTO and mapping values manually

Regards,
Amit