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
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
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)
Thanks
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-conte...
Thanks
Hi Mukesh, thanks for the reply.
Yes, I already had checked that link. But I want all the selected fragments in one json file.
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)
Thanks
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.
@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-s...
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:
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.
@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!
Views
Replies
Total Likes
Views
Likes
Replies