Thursday 22 June 2017

AEM Content Fragment output as JSON

AEM Content Fragments can be accessed through JSON file URL's. This helps to verify the data while authoring.

To ensure the JSON format is enabled follow below steps:

To enable the content fragment JSON, we need to enable /system/console/configurations > AEM Content Service Feature Flag > (Select) Enable AEM Content Services check box.


YouTube demo videos for Content Fragments:
         AEM 6.3 Content Fragments Basics
         Content Fragments AEM
         View Content fragment output in aem

Say we have created 'this-is-my-frag' Content Fragment,

We can see the complete content using infinity json as url.
http://localhost:4502/content/dam/this-is-my-frag.infinity.json

If we want to generate its output as caas way below url helps.
http://localhost:4502/content/dam/this-is-my-frag/_jcr_content.caas.json

{
    "_children": [{
        "name": "jcr:content",
        "title": "This is my frag",
        "path": "/content/dam/this-is-my-frag/jcr:content",
        "type": "dam:AssetContent",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content.caas.json"
    }]
}

Now when we load the children from above _jcr_content.caas.json

{
    "title": "This is my frag",
    "dam:assetState": "processed",
    "description": "Hello there",
    "contentFragment": true,
    "lastFragmentSave": "Wed Jun 21 2017 15:29:39 GMT+0530",
    "dam:relativePath": "this-is-my-frag",
    "_children": [{
        "name": "renditions",
        "path": "/content/dam/this-is-my-frag/jcr:content/renditions",
        "type": "nt:folder",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/renditions.caas.json"
    }, {
        "name": "related",
        "path": "/content/dam/this-is-my-frag/jcr:content/related",
        "type": "nt:unstructured",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/related.caas.json"
    }, {
        "name": "associated",
        "path": "/content/dam/this-is-my-frag/jcr:content/associated",
        "type": "sling/collection",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/associated.caas.json"
    }, {
        "name": "metadata",
        "path": "/content/dam/this-is-my-frag/jcr:content/metadata",
        "type": "nt:unstructured",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/metadata.caas.json"
    }, {
        "name": "model",
        "title": "Simple Fragment",
        "path": "/content/dam/this-is-my-frag/jcr:content/model",
        "type": "nt:unstructured",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/model.caas.json"
    }]
}

Now we can iterate through the children in above json and get corresponding renditions, related, associated, metadata, model

For eg: model gives below json
{
    "title": "Simple Fragment",
    "version": 2,
    "description": "A simple fragment, containing one single element and no predefined variations",
    "precreateElements": true,
    "_children": [{
        "name": "elements",
        "path": "/content/dam/this-is-my-frag/jcr:content/model/elements",
        "type": "nt:unstructured",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/model/elements.caas.json"
    }, {
        "name": "variations",
        "path": "/content/dam/this-is-my-frag/jcr:content/model/variations",
        "type": "nt:unstructured",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/model/variations.caas.json"
    }]
}

where the variations from above link loads

{
    "_children": [{
        "name": "variation2",
        "title": "variation2",
        "path": "/content/dam/this-is-my-frag/jcr:content/model/variations/variation2",
        "type": "nt:unstructured",
        "href": "http://localhost:4502/content/dam/this-is-my-frag/_jcr_content/model/variations/variation2.caas.json"
    }]
}

This way we can iterate through the content fragment JSON.

https://www.youtube.com/channel/UCbDTGaDneAbj_RCX27VE4cA/videos



Subscribe Our YouTube Channel Here.


Related Posts

AEM Leading to head less CMS?
AEM Content Fragment output as JSON
AEM 6.3 Content Fragments Basics
How to create a Content Fragment? step by step tutorial
Create & Access the content fragment programmatically


No comments:

Post a Comment