Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to create JSON for list of Content Fragments?

Avatar

Level 4

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

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

7 Replies

Avatar

Community Advisor

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

Avatar

Level 4

Hi Mukesh, thanks for the reply.

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

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

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


Aanchal Sikka

Avatar

Level 2

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


Avatar

Level 4

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.

Avatar

Administrator

@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