I'm curious about the best practice for applying JSON-LD onto a site for schema.org.
If I have a page with an Article
and I also want to define WebSite
on my page, I would have this:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://www.example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://www.example.com/search?&q={query}",
"query-input": "required"
}
}
</script>
<!- … -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Article",
"author": "John Doe",
"interactionCount": [
"UserTweets:1203",
"UserComments:78"
],
"name": "How to Tie a Reef Knot"
}
</script>
Is this correct or wrong? Is there any benefit or need to merge these into the same script or array of items?
It’s valid. You can have as many data blocks (=
script
elements) as you wish.A possible benefit of using only one
script
element: it allows to make relationships between multiple items easier (e.g., should you decide to usehasPart
ormainEntity
), as you simply have to nest the items.But making these relationships is of course also possible when using separate data blocks, by referencing the URI of the item with
@id
(thanks, @ Gregg Kellogg).(For reference, adding two or more top-level items in a single
script
is possible with@graph
.)