How to create JSON for list of Content Fragments? | Community
Skip to main content
sesmic
Level 4
July 12, 2024
Solved

How to create JSON for list of Content Fragments?

  • July 12, 2024
  • 5 replies
  • 2996 views

Hi Team,

I have a requirement where I need to generate the single JSON data of all the content fragments selected. In component dialog Author is selecting the folder & via Sling Model I'm able to get all the content fragments within selected folder. Then I'm adding to those in list<resource> & can use on page as well.

But now I want to generate one JSON file with all the data from these fragments (something like below snippet) & each fragment may include more than 20 fields.

{ "cfList": [ { "id": "1", "firstName": "A", "lastName": "B" }, { "id": "2", "firstName": "C", "lastName": "D" }, { "id": "3", "firstName": "E", "lastName": "F" } ] }

Can you please suggest any approach to achieve this?

Many thanks,

sesmic

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 MukeshYadav_

Hi @sesmic ,

Could you please elaborate one json file.

If you mean to create a file as you are already have a json object(data)

  • you may use Java File class,
  • if you want to create file in content structure you may use jcr api, or
  • if you want to send as servlet you may write in application/json in response body / use model exporter as mentioned in aanchal-sikka reply

Thanks

5 replies

MukeshYadav_
Community Advisor
Community Advisor
July 12, 2024

Hi @sesmic ,

By default content fragment are exposed as json and we can also use GraphQL for selected cf

Hope you had a look https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-get-data-from-content-fragment-and-export-in-json-format/m-p/564867

 

Thanks

sesmic
sesmicAuthor
Level 4
July 13, 2024

Hi Mukesh, thanks for the reply.

Yes, I already had checked that link. But I want all the selected fragments in one json file.

MukeshYadav_
Community Advisor
MukeshYadav_Community AdvisorAccepted solution
Community Advisor
July 14, 2024

Hi @sesmic ,

Could you please elaborate one json file.

If you mean to create a file as you are already have a json object(data)

  • you may use Java File class,
  • if you want to create file in content structure you may use jcr api, or
  • if you want to send as servlet you may write in application/json in response body / use model exporter as mentioned in aanchal-sikka reply

Thanks

aanchal-sikka
Community Advisor
Community Advisor
July 14, 2024

@sesmic 

 

Have you tried the Sling Model Exporter: allows new annotations to be added to Sling Models that define how the Model an can be exported as a different Java object, or more commonly, serialized into a different format such as JSON.

https://experienceleague.adobe.com/en/docs/experience-manager-learn/foundation/development/develop-sling-model-exporter 

Aanchal Sikka
arko_martech
Adobe Champion
Adobe Champion
July 14, 2024

@sesmic If you can get all the content fragment paths inside the folder you selected in the dialog.
Use grapthQL to get the content fragment data through a graphQL query and store them in a json array. Add that array to a JSON object having the cflist as a key.
Export the object as String by gson.toJson by using a getter and making it used in sightly or use the model as sling model exporter.

You can define it too now that what kind of object you wanna export.
Doc-https://experienceleague.adobe.com/en/docs/experience-manager-learn/foundation/development/develop-sling-model-exporter


PGURUKRISHNA
Level 4
July 15, 2024

Hi @sesmic 

To generate a single JSON file with all the data from the content fragments, you can create a Java class that iterates over the list of resources (content fragments) and extracts the required fields.

Here's a suggested approach:

  1. Create a Java class that takes the list of resources as input.
  2. Iterate over the list of resources and extract the required fields using the getValueMap() method.
  3. Create a JSON object for each content fragment and add it to a JSON array.
  4. Finally, write the JSON array to a file.

import org.apache.sling.api.resource.Resource;
import org.json.JSONArray;
import org.json.JSONObject;

public class ContentFragmentToJson {
public void generateJson(List<Resource> contentFragments) throws IOException {
JSONArray cfList = new JSONArray();
for (Resource contentFragment : contentFragments) {
ValueMap properties = contentFragment.getValueMap();
JSONObject cfJson = new JSONObject();
cfJson.put("id", properties.get("id", ""));
cfJson.put("firstName", properties.get("firstName", ""));
cfJson.put("lastName", properties.get("lastName", ""));
// Add more fields as required
cfList.put(cfJson);
}
String jsonOutput = cfList.toString();
// Write the JSON output to a file
FileWriter fileWriter = new FileWriter("output.json");
fileWriter.write(jsonOutput);
fileWriter.close();
}
}

 

Note that you'll need to modify the code to extract the required fields from the content fragments and add them to the JSON object. Additionally, you may want to handle errors and exceptions more robustly.

Once you have the JSON file generated, you can use it as needed in your application.

kautuk_sahni
Community Manager
Community Manager
July 16, 2024

@sesmic Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni