Facing issue with Exporter while exporting with JSONObject on page getting unwanted format | Community
Skip to main content
keshava219
June 17, 2022
Solved

Facing issue with Exporter while exporting with JSONObject on page getting unwanted format

  • June 17, 2022
  • 1 reply
  • 1021 views

Facing issue with Exporter while exporting with JSONObject on page getting unwanted format like 

Getting :

 

 "options" : {
                        "id-option-1" : {
                          "altText" : {
                            "chars" : "default:asset:description",
                            "string" : "default:asset:description",
                            "valueType" : "STRING"
                          },
                          "description" : {
                            "chars" : "\u003Cp\u003ESample Descritption\u003C/p\u003E",
                            "string" : "\u003Cp\u003ESample Descritption\u003C/p\u003E",
                            "valueType" : "STRING"
                          }
                        },
                        "id-option-2" : {
                          "altText" : {
                            "chars" : "default:asset:description",
                            "string" : "default:asset:description",
                            "valueType" : "STRING"
                          },
                          "description" : {
                            "chars" : "\u003Cp\u003Esample description 2\u003C/p\u003E",
                            "string" : "\u003Cp\u003Esample description 2\u003C/p\u003E",
                            "valueType" : "STRING"
                          }
                        },

required format:

options: {
'id-option-1': {
description: 'Some description 2',
buttonText: 'This is the option 1 text',
},
'id-option-2': {
description: 'Some description 2',
buttonText: 'The is the option 2 text',
},
'id-option-3': {
description: 'Some description 3',
buttonText: 'This is the option 3 text',
},
'id-option-4': {
description: 'Some description 4',
buttonText: 'This is the option 4 text',
},
'id-option-5': {
description: 'Some description 5',
buttonText: 'This is the option 5 text',
},
'id-option-6': {
description: 'Some description 6',
buttonText: 'This is the option 6 text',
},
'id-option-7': {
description: 'Some description 7',
buttonText: 'This is the option 7 text',
},
'id-option-8': {
description: 'Some description 8',
buttonText: 'This is the option 8 text',
},
},

 

JAVA method in Exporter:

 

 

public JsonObject getOptions() {
JsonObjectBuilder jsonkey = Json.createObjectBuilder();

int i = 1;
for (Resource resource: content) {

TreeModel resourceValues = resource.adaptTo(TreeModel.class);
JsonObjectBuilder keynewsArticleJSON = Json.createObjectBuilder();

//keynewsArticleJSON.add("fileReference", (String) resourceValues.getFileReference());

keynewsArticleJSON.add("altText", (String) resourceValues.getAltText());

keynewsArticleJSON.add("description", (String) resourceValues.getDescription().toString());

log.info("filesResference:::::::::::" + resourceValues.getFileReference());

jsonkey.add("id-option-" + i, keynewsArticleJSON.build());
i++;
}

log.info("jsonSsdtring::::::::::::::" + jsonkey.build());

String jsonString = jsonkey.build().toString();
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));

JsonObject object = jsonReader.readObject();

return object;
}

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 SantoshSai

@keshava219 If you annotated your model already with @Exporter(name="jackson", extensions="json) then no need to convert it additionally, you will get your result rendered in json. Please check above link.

1 reply

SantoshSai
Community Advisor
Community Advisor
June 17, 2022

Hi @keshava219 ,

 

I can see you have object class already TreeModel, why don’t you use Sling Model Exporter? Using Jackson Exporter Annotation that’s easy to export data in json format instead manual generating json data.

I would recommend you to please check this link https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/develop-sling-model-exporter.html?lang=en

 

Regards,

Santosh

Santosh Sai
keshava219
June 17, 2022

Hi @santoshsai ,

 

           I'm using sling model exporter only , that was a method inside the exporter. the annotations in exporter im using the below 

 

@Model(adaptables = { SlingHttpServletRequest.class }, adapters = { current.class,
ComponentExporter.class }, resourceType = {
"RESOURCE_TYPE" }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION, options = {
@ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "false") })

@JsonSerialize(as = current.class)

public class Samplemodel extends ComponentID implements ComponentExporter {

 

By using the method im trying to generate JSONobject based on my requirement as samples in above 

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
June 17, 2022

@keshava219 If you annotated your model already with @Exporter(name="jackson", extensions="json) then no need to convert it additionally, you will get your result rendered in json. Please check above link.

Santosh Sai