如何组合多个 JSON-LD 标记?

How do you combine several JSON-LD markups?

作为新手,我发现很难将几个或多个 JSON-LD 标记合并在一起。你能告诉我我做错了什么吗?

当我在 Google 结构化数据测试工具中输入以下标记时,它只显示 Organization 模式类型的结果,同时还有 BreadcrumbList 类型。

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"legalName": "Example INC",
"logo": "https://www.example.com/image.png",
"url": "https://www.example.com/",
"sameAs": [
"https://www.facebook.com/example",
"https://www.linkedin.com/company/example",
"https://twitter.com/example",
"https://www.youtube.com/user/example",
"https://en.wikipedia.org/wiki/example"
]
}
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": "1",
"item": {
"@id": "https://www.example.com/",
"name": "Homepage" 
}
}
]
</script>

JSON 连接不正确。如果您有一个启用了 JSON 的编辑器(带有 lint),如果您手动执行此操作,那将非常有帮助。 Atom (https://atom.io) 是个不错的选择。

对于这个特定的例子,这里是更正后的版本:

[{
    "@context": "http://schema.org",
    "@type": "Organization",
    "legalName": "Example INC",
    "logo": "https://www.example.com/image.png",
    "url": "https://www.example.com/",
    "sameAs": [
        "https://www.facebook.com/example",
        "https://www.linkedin.com/company/example",
        "https://twitter.com/example",
        "https://www.youtube.com/user/example",
        "https://en.wikipedia.org/wiki/example"
    ]}, {
    "@type": "BreadcrumbList",
    "itemListElement": [{
        "@type": "ListItem",
        "position": "1",
        "item": {
            "@id": "https://www.example.com/",
            "name": "Homepage"
        }
    }]}
]

PS:通常简单地正确格式化代码可以帮助发现简单的错误,因为这些错误。

要指定多个顶级项目,您有以下三种选择:

数组

<script type="application/ld+json">
[
  {
     "@context": "http://schema.org",
     "@type": "Organization"
  },
  {
     "@context": "http://schema.org",
     "@type": "BreadcrumbList"
  }
]
</script>

缺点:您必须为每个项目重复 @context

@graph

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@graph": 
  [
    {
       "@type": "Organization"
    },
    {
       "@type": "BreadcrumbList"
    }
  ]
}
</script>

多个 script 元素

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Organization"
}
</script>

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList"
}
</script>

缺点:您必须为每个项目重复 script 元素和 @context


但通常最好只提供一个顶级项目,并将其他项目嵌套在合适的属性下。但这并非在所有情况下都可行。

在您的情况下,似乎可以通过添加 WebPage 项来实现,假设它是组织的页面并且该页面具有面包屑列表:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "WebPage",
  "provider": 
  {
    "@type": "Organization"
  },
  "breadcrumb": 
  {
    "@type": "BreadcrumbList"
  }
}
</script>

(You can achieve the same without nesting: 给每个项目一个带有 @id 的 URI,然后将这些 URI 引用为 属性 值。)