Export a component to JSON with multifield elements and display contact list in JSON | Community
Skip to main content
Level 2
February 29, 2024
Solved

Export a component to JSON with multifield elements and display contact list in JSON

  • February 29, 2024
  • 3 replies
  • 1021 views

Hello, I want to know how I can export a component to json when it has multifield elements

I have these classes but I don't really know how to display the contact list elements in the json

 

package com.program.core.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Exporter; import org.apache.sling.models.annotations.ExporterOption; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Via; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import javax.inject.Inject; import java.util.List; @Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) @Exporter(name = "jackson", extensions = "json", options = { @ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true"), @ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "false") }) public class ContactCardsModel { @SlingObject SlingHttpServletRequest slingRequest; @586265 @2434638("resource") public List<Contact> contact; public List<Contact> getContact() { return contact; } }

 

 

 

package com.program.core.models; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import javax.inject.Inject; @Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class Contact { @586265 private String contactName; @586265 private String contactEmail; public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } public String getContactEmail() { return contactEmail; } public void setContactEmail(String contactEmail) { this.contactEmail = contactEmail; } }

 

 I would really appreciate if someone could give me an idea how to do it.

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 EstebanBustamante

Hi, 

 

You can find an example here of how to do it: https://gist.github.com/Surajkamdi/6d51779d62278b56a747e04532f39036 

Pay attention to this code:

//Multifield Child Resource for social links @ChildResource(name = "socialLinks") Collection<SocialLinksModel> socialLinks;

 

You can check how this work here: https://medium.com/@toimrank/aem-sling-model-exporter-90dc872835b9 

 

Hope this helps.

 

3 replies

krati_garg
Adobe Employee
Adobe Employee
February 29, 2024

@lidermangiraldoescobar 

Can you share what response are you expecting Vs what you are getting when calling AEM Content Services:
http://localhost:4502/content/practice/us/en/test.model.tidy.json

 

here test is a page, where you might have authored your component ContactCard

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
February 29, 2024

Hi, 

 

You can find an example here of how to do it: https://gist.github.com/Surajkamdi/6d51779d62278b56a747e04532f39036 

Pay attention to this code:

//Multifield Child Resource for social links @ChildResource(name = "socialLinks") Collection<SocialLinksModel> socialLinks;

 

You can check how this work here: https://medium.com/@toimrank/aem-sling-model-exporter-90dc872835b9 

 

Hope this helps.

 

Esteban Bustamante
kautuk_sahni
Community Manager
Community Manager
March 7, 2024

@lidermangiraldoescobar Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni