How to combine structured data into one script tag | RJS

How to combine structured data into one script tag

Ralph J. Smit Laravel & PHP-developer.

Adding structured data to pages on this blog has really helped improve my rankings. But sometimes you want to combine two or more 'blocks' of schema markup in the same <script> tag. On this page, I'll show you how to combine multiple structured data elements into one declaration.

What is the 'classic' way to add structured data to a page? A single declaration is added in a <script> tag with a type of ld+json. If you want to add multiple declarations, you'd just repeat the pattern with a new <script> tag for each item:

<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
...
}
</script>
 
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
...
}
</script>
 
<!-- You'd need to repeat this pattern for each declaration.
Not very efficient... -->

How to combine multiple structured data into one script tag?

To do this, it turns out that we need to use a special JavaScript/JSON function, called @graph. The @graph function allows us to rewrite our whole code so that it neatly fits within one <script> tag. Let's have a look. ✌️

<script type="application/ld+json">
 
{
"@context": "http://schema.org",
"@graph": [
{
"@type": "FAQPage",
...
}, {
"@type": "BreadcrumbList",
...
} //No comma after the last one!
] //No comma here either!
}
 
</script>
 
<!-- Aaah... way better. -->

What @graph does, is that it enables us to provide each structured data block as an array. For each markup, add it inside the "@graph": [] declaration. Make sure for each markup to remove the @context declaration, as that one is already specified above.

_Note: I would always recommend validating your structured data with the Rich Results Tester from Google. In this way, you're certain that you're not adding invalid markup to your website. I do not know whether you're punished for invalid data on your webpage. If you do know whether or not this is the case, please let me know in the comments! πŸ‘‡_πŸ™

I always enjoy hearing from you, so if this has been useful or if you have a problem, let me know below!

Published by Ralph J. Smit on in Guides. Last updated on 10 March 2022.


Hey!πŸ‘‹ Like what you read?

Every now and then I send out a newsletter with new articles, tutorials and packages, mainly related to PHP in general and Laravel.

Subscribe to the newsletter and stay up to date with all things Laravel, Tailwind, WordPress & PHP. Never miss an update.πŸ™‹β€