Expand my Community achievements bar.

Want to merge 2 Content Fragment and get JSON

Avatar

Level 4

Dear All,

 

I have one customized content fragment model called smartwidget as shown below.

 

************************************** START ***********************************************

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:granite="http://www.adobe.com/jcr/granite/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:Template"
allowedPaths="[/content/entities(/.*)?]"
ranking="{Long}100">
<jcr:content
cq:lastModified="{Date}2020-05-11T10:51:40.812-05:00"
cq:lastModifiedBy="admin"
cq:scaffolding="/conf/content-fragment-models/settings/dam/cfm/models/smartWidget/jcr:content/model"
cq:templateType="/libs/settings/dam/cfm/model-types/fragment"
jcr:description="Author Landing Pages Content Pages"
jcr:primaryType="cq:PageContent"
jcr:title="Smart-Widget"
sling:resourceSuperType="dam/cfm/models/console/components/data/entity"
sling:resourceType="dam/cfm/models/console/components/data/entity/default">
<model
cq:targetPath="/content/entities"
jcr:primaryType="cq:PageContent"
sling:resourceType="wcm/scaffolding/components/scaffolding"
dataTypesConfig="/mnt/overlay/settings/dam/cfm/models/formbuilderconfig/datatypes"
maxGeneratedOrder="20">
<cq:dialog
jcr:primaryType="nt:unstructured"
sling:resourceType="cq/gui/components/authoring/dialog">
<content
jcr:lastModified="{Date}2020-05-11T10:51:40.812-05:00"
jcr:lastModifiedBy="admin"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/fixedcolumns">
<items
jcr:primaryType="nt:unstructured"
maxGeneratedOrder="24">
<widgetSectionLabel
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Enter the Widget section label "
fieldDescription="Enter the Widget section label"
fieldLabel="Widget Section Label"
maxlength="40"
metaType="text-single"
name="widgetSectionLabel"
renderReadOnly="false"
showEmptyInReadOnly="true"
valueType="string"/>
<actionWidget
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
composite="{Boolean}true"
fieldLabel="Action Widget"
metaType="text-single"
name="actionWidget"
renderReadOnly="false"
valueType="string[]">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/container"
name="./actionWidget">
<items jcr:primaryType="nt:unstructured">
<title
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Enter or update the title for Action Widget"
fieldDescription="Enter or update the title for Action Widget"
fieldLabel="Widget Title *"
name="title"
renderReadOnly="false"
showEmptyInReadOnly="true"
valueType="string"/>
<widgetName
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Enter the Name used for Action Widget"
fieldDescription="Enter the Name used for Action Widget"
fieldLabel="Widget Name"
name="widgetName"/>
<widgetId
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
emptyText="Enter the ID for Action Widget"
fieldDescription="Enter the ID for Action Widget"
fieldLabel="Widget ID"
name="widgetId"/>
</items>
</field>
</actionWidget>
<contentReference
jcr:primaryType="nt:unstructured"
sling:resourceType="dam/cfm/models/editor/components/fragmentreference/multifield"
fieldLabel="smartWidget Reference"
filter="hierarchy"
fragmentmodelreference="/conf/content-fragment-models/settings/dam/cfm/models/smartWidget"
listOrder="9"
metaType="fragment-reference"
name="smartwidgetcfreference"
nameSuffix="contentReference"
renderReadOnly="false"
rootPath="/content/dam/content-admin/static/global/en/todays/smartwidget"
showEmptyInReadOnly="true"
valueType="string/content-fragment[]">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="dam/cfm/models/editor/components/fragmentreference"
fragmentmodelreference="/conf/content-fragment-models/settings/dam/cfm/models/smartWidget"
name="smartwidgetcfreference"
renderReadOnly="false">
<granite:data
jcr:primaryType="nt:unstructured"
mvmaxitems="5"/>
</field>
</contentReference>
</items>
</content>
</cq:dialog>
</model>
</jcr:content>
</jcr:root>

 

************************************** END *******************************************

 

And I have created 2 CF by using above Model and below is the JSON

 

*********************   First TESTING1 JSON *********************************************

{
"widgetSectionLabel": "testing1",
"actionWidget": [
{
"widgetName": "displayConditions1",
"widgetId": "display_Conditions1",
"title": "Testing1"
}
]
}

 

*********************** Second testwidget JSON *****************************************

{
"widgetSectionLabel": "testing1",
"actionWidget": [
{
"widgetName": "displayConditions1",
"widgetId": "display_Conditions1",
"title": "Testing1"
}
]
}

 

Now my requirement is that I want to merge the above 2 JSONS (means 2 content fragments) and want to make one JSON but when I am doing so I am not getting any JSON response as below

 

subnaik_0-1733196064076.png

 

subnaik_1-1733196261384.png

 

 

Topics

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

6 Replies

Avatar

Level 5

Hi @subnaik 

 

Have you written custom servlet to get the JSON response for content fragment data ?

 

If yes, please check if there is any errors in logs while iterating through content fragment data 

 

Thanks 

Avatar

Level 4

Yes we have written custom servlet for the JSON.

Avatar

Level 5

Hi,

 

Get the jsonObject for the content fragments, and merge them like below in your custom servlet -

 

JSONObject Obj1 = (JSONObject) json1.get("cf1"); //jsonObject for 1st content fragment
JSONObject Obj2 = (JSONObject) json2.get("cf2"); //jsonObject for 2nd content fragment
JSONObject merge = new JSONObject();
merge.put("Object1", Obj1);
merge.put("Object2", Obj2);

If you facing issues, please post log details if any

 

Thanks

 

Avatar

Level 7

Hi @subnaik 

 

First I would say that proper wording is not that you wantvyo merge two content fragments (that is not possible) but rather you want to aggregate the JSON representation of each into a single JSOn and return that.

 

I have a confusion about the relationship between what I see in first picture and what Insee in second picture.

 

In the second picture I see you have a servlet. I assume here you have implemented a logic that looks up for the two content fragments based on the selectors you passes down to the servlet. You need to do a debug (in parallel with checking the logs) and see where it crashes. The execution chain is large so in any point you could have ommited something. Maybe you did not return properly the servlet response. Maybe you have a hidden NPE. Mayne your CFs could not be found by the code and so on.

 

Now about the first picture ? Do you use that to combine your CFs data? What is that? Is it a simple AEM component or another type of content fragment like an aggregetor of the first type ? Do you have a Sling model mapping on them or how do you use it to return aggregated JSON ?

 

 

Avatar

Level 4

Hi Please see my response in below.

 

First I would say that proper wording is not that you want to merge two content fragments (that is not possible) but rather you want to aggregate the JSON representation of each into a single JSOn and return that.

@subnaik - Yes I want to merge also 2 content fragments in one content fragment by using fragment reference in the model, as shown below.

subnaik_0-1733231401358.png

 

We have already written custom servlet which is coming fine without using the fragment reference in the content fragment model, as shown below.

 

subnaik_1-1733231741339.png

 

But when I put the fragment reference in the model and trying to hit the same servlet then it is not coming fine and coming below error.

 

subnaik_2-1733232129797.png

 

Avatar

Level 7

Hi @subnaik 

 

Allow me to consider that referencing two CF in parent CF is still not the same as merging two CF. Unless you take the two JCR nodes and make one out of them, it is not called merging, but rather data aggregation. But again, this is only my view, aside from the actual issue you are raising.

As for the issue itself, now the first picture you shared makes sens. Can you share some code snippet to show how exactly are you reading the parent CF in your servlet ? Also if you have logs entries would be helpful ? The more info you provide the better help you can get. It definitely looks like a code issue as long as without references it is working, and with referencing is not.