Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 2

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;
}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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.

View solution in original post

4 Replies

Avatar

Community Advisor

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-slin...

 

Regards,

Santosh

Avatar

Level 2

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 

Avatar

Correct answer by
Community Advisor

@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.

Avatar

Level 2

So , Cant we return JSONObject if we give annotations like above?