Loop through CMS collection with custom code

Hello everybody,

I need to implement FAQ schema markup to my FAQ page, here’s what the mockup code would look like:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "title",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "answer"
    }
  },{
    "@type": "Question",
    "name": "`",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": ""
    }
  }]
}
</script>

Is it possible to generate a question object for each item in the collection? Can I loop through the collection with custom code and access fields?

Thanks