Looping JSON objects?

Hi!

I am trying to figure out if it is possible to loop code, such as JSON.

In theory, I could place this part in one Raw element:

<script type="application/ld+json">

And then the </script> in the last Raw.

It would be amazing if we could have a looped raw or something, to loop objects inside. The commas between the objects could be easily shown with the Index > Is not > Last rule.

This way we could dynamically display all kinds of Schema markup, such as FAQs.

2 Likes

Howdy. I guess it depends on what you are trying to do. I do see the use case for a “raw container” type of element though. You can kinda get by using Div tag and changing the tag to “script” in breakout mode. Some of your later parts seem like another templater request. Twig / Liquid or whatever we use is definitely something I’d still love to do and roughly started a while ago. So much potential when you factor in looping inside a template.

If you just need JSON data from a looper. If you do type="json" it’ll output json. Which you could use in a script tag like so, or just from JS. If your trying to build JSON dynamically this doesn’t really help though without using a custom looper or dynamic content call.

Hope this helps!

image

var data = {{dc:p:data type="json"}};

data.forEach(console.log);
1 Like

Amazing, I will play with that.

A use case could be this, for example:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "lorem ipsum"
    }
  },{
    "@type": "Question",
    "name": "",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Dolor sit amet"
    }
  },{
    "@type": "Question",
    "name": "",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Lorem ipsum"
    }
  },{
    "@type": "Question",
    "name": "",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": ""
    }
  }]
}
</script>

So in this example, we have three FAQ questions with answers. Its main entity should be generated dynamically, with a comma between them. The last one must be without a comma for the code to stay valid.

This way, we could have a FAQ page with an accordion looper for example that generates the visible FAQs, but at the same time the second looper is generating the JSON-LD FAQ structured data.

There are many other Structured data opportunities to utilize with this approach, covering things that the native Snippet addon cannot do.

Thanks!

1 Like