We need to write a script to create a JSON-LD script for structured Data for a Component in JSP let say FAQ component, we have written script to generate JSON LD for Structured data,
faq.jsp :
<script type="application/ld+json">
{
"@context": "https://schema.org",
"type": "FAQPage",
"mainEntity": [
<c:forEach var='questionItem' items='${faq.faqQuestionList}' varStatus='itemsLoopSchema'>
{
<c:set var="trimmedAnswer" value="${fn:trim(questionItem.answer)}" />
"@type": "Question",
"name": "${questionItem.question}",
"acceptedAnswer": {
"@type": "Answer",
"text": "${fn:escapeXml(trimmedAnswer)}"
}
<c:choose>
<c:when test="${itemsLoopSchema.last}">
}
</c:when>
<c:otherwise>
},
</c:otherwise>
</c:choose>
</c:forEach>
]
}
</script>
Now when this JSP Component included Multiple time the script will run multiple time and create multiple JSON
Instead I want to load all the multiple FAQ component and then create JSON (or run script only once).
A single JSON LD where all the details will be there for multiple faq.jsp
*Restricting Script to run once and get the data of all the page DOM in JSON for SEO purpose in JSP
2
Answers
I know this may not be the answer you’re looking for but having multiple json+ld blocks on the same page is perfectly valid, even if they’re of the same type.
Additionally, I would recommend inlining the structured data into the FAQ markup you’re outputting using microformats (using
itemscope
,itemprop
, anditemtype
attributes).Some ideas:
Add a Sling Model in the head of your page. query all FAQ components and generate the JSON+LD data. Sling Model just outputs the string
Create a FAQ Container where you drop each FAQ component. FAQWrapper Sling Model generates the JSON+LD just as above
Also, some recommendations: