Join the Webstudio community

Updated 7 days ago

Hello,I have a dynamic FAQ component that pulls

Hello,

I have a dynamic FAQ component that pulls question–answer pairs from my database and renders them correctly on the page. Now I need to output a valid FAQPage JSON-LD schema so Google can recognize my FAQ rich result, but I think WebStudio doesn’t allow me to insert Collection tags inside a <script type="application/ld+json"> block.

What’s the best way to generate and inject the following schema dynamically (based on my rendered FAQs) without hard-coding every question and answer?

{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Where are you located?",
"acceptedAnswer": {
"@type": "Answer",
"text": "We are based in Paris, France."
}
},
{
"@type": "Question",
"name": "Do you offer private sailing lessons?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, private lessons are available on request."
}
}
/* …and so on for each FAQ item… */
]
}

Has anyone implemented a solution (JavaScript embed or WebStudio workaround) that reads the rendered FAQ items and automatically generates this <script type="application/ld+json"> block? Any code snippets or step-by-step guidance would be greatly appreciated.
M
a
O
15 comments
are you allowed to loop FAQ selectors
Using query selector all to get the FAQ questions and answers
loop through each FAQ item and build the schema using the FAQ item.question.textcontent
make sure you trim it so it is exactly as the rendered text
that's the idea. I haven't not tested but let's hope it works.
Here's a GitHub gist to the code, couldn't pastw the whole code in discord
prompted AI and it gave me something that looks promising.

Make you have the selectors in html as well.
Mikeoxygen - Thank you for your reply! Yes, that's the way I'm going now, generate faq shema with js based on what's on the page.

But I have a legitimate question, maybe there is a way to do it more nicely directly in Webstudio
sounds like you want to build a json of a specific structure based on a response from the api in a different stucture, esstially this is about transforming one object into another object, is that correct?
if yes, ideal place to do this is backend
specific endpoint that returns you the schema
Thanks for your hints friends! Finally solved the problem with this script, which is inserted into the HTML embed in the FAQ block on the page and the webstudio outputs the FAQ page array right there. Probably not the most elegant solution, but it seems to work.

<script> function generateFaqSchema() { var results = ${FAQ$32$.data.results}; if (!Array.isArray(results) || results.length === 0) { return; } var faqItems = []; results.forEach(function(item) { var questionText = item["field_2660140"]; var answerText = item["field_2660146"]; if (questionText && answerText) { faqItems.push({ "@type": "Question", "name": questionText.trim(), "acceptedAnswer": { "@type": "Answer", "text": answerText.trim() } }); } }); if (faqItems.length > 0) { var faqSchema = { "@context": "https://schema.org", "@type": "FAQPage", "mainEntity": faqItems }; var oldSchema = document.querySelector('script[type="application/ld+json"][data-schema-type="FAQPage"]'); if (oldSchema) { oldSchema.remove(); } var scriptTag = document.createElement('script'); scriptTag.type = 'application/ld+json'; scriptTag.setAttribute('data-schema-type', 'FAQPage'); scriptTag.textContent = JSON.stringify(faqSchema, null, 2); document.head.appendChild(scriptTag); } } try { generateFaqSchema(); } catch (e) { } </script>
Yeah, this is purely client side, but could work well enough
Add a reply
Sign up and join the conversation on Discord
","upvoteCount":0,"dateCreated":"2025-05-12T11:20:59.704Z","datePublished":"2025-05-12T11:20:59.704Z","dateModified":"2025-05-12T11:20:59.704Z","url":"https://help.webstudio.is/helloi-have-a-dynamic-faq-component-that-pulls-q7P2wz901kQP#037f2c0d-c950-4ec8-9a46-830c1a66b20a","author":{"@type":"Person","url":"https://help.webstudio.is/members/4733ff8a-a321-40b7-8d91-f1f7b169bfad","name":"alex FR","identifier":"4733ff8a-a321-40b7-8d91-f1f7b169bfad","image":"https://cdn.discordapp.com/avatars/720899840950927461/4030c2daeb6afd4be141070ccd89b795.webp?size=256"}},{"@type":"Answer","text":"Yeah, this is purely client side, but could work well enough","upvoteCount":0,"dateCreated":"2025-05-12T11:58:33.732Z","datePublished":"2025-05-12T11:58:33.732Z","dateModified":"2025-05-12T11:58:33.732Z","url":"https://help.webstudio.is/helloi-have-a-dynamic-faq-component-that-pulls-q7P2wz901kQP#32841a57-fa65-4ba3-9ee4-a30dd5419264","author":{"@type":"Person","url":"https://help.webstudio.is/members/cdd3ed87-53a0-414a-885a-56b7939e412a","name":"Oleg Isonen","identifier":"cdd3ed87-53a0-414a-885a-56b7939e412a","image":"https://cdn.discordapp.com/avatars/469405813048606720/8b66a5882214c63ee6148fcce3ef8e93.webp?size=256"}}]}}