JSON-LD框架:强制重复冗余属性值

时间:2018-09-22 19:53:42

标签: parsing json-ld

是否可以强制复制冗余属性值,例如authorcreator

目标是减少以后的额外解析,并拥有一个易于遍历的JSON对象,无论重复的值如何。

示例:

{
  "@type": "NewsArticle",
  "articleBody": "Article Body",
  "author": {
    "id": "_:b1"
  },
  "creator": {
    "id": "_:b1",
    "type": "Person",
    "name": "Creator Name",
    "url": "https://example.org/author/creator-name/"
  },
  "description": "Description.",
  "headline": "Headline"
}

框架:

{
  "@context": "https://schema.org/docs/jsonldcontext.json",
  "@vocab": "https://schema.org",
  "@type": ["Article", "NewsArticle", "TechArticle", "ScholarlyArticle"],
  "author": {
    "@type": "http://schema.org/Person",
    "@embed": "true"
  }
}

预期结果:

{
    "@type": "NewsArticle",
    "articleBody": "Article Body",
    "author": {
      "id": "_:b1",
      "type": "Person",
      "name": "Creator Name",
      "url": "https://example.org/author/creator-name/"
    },
    "creator": {
      "id": "_:b1",
      "type": "Person",
      "name": "Creator Name",
      "url": "https://example.org/author/creator-name/"
    },
    "description": "Description.",
    "headline": "Headline"
  }

1 个答案:

答案 0 :(得分:4)

要重复一个节点,您想使用"@embed": "@always"。尝试this update to your example on the JSON-LD Playground

输入:

{
  "@context": "https://schema.org/docs/jsonldcontext.json",
  "@type": "NewsArticle",
  "articleBody": "Article Body",
  "author": {
    "id": "_:b1"
  },
  "creator": {
    "id": "_:b1",
    "type": "Person",
    "name": "Creator Name",
    "url": "https://example.org/author/creator-name/"
  },
  "description": "Description.",
  "headline": "Headline"
}

框架:

{
  "@context": "https://schema.org/docs/jsonldcontext.json",
  "@type": ["Article", "NewsArticle", "TechArticle", "ScholarlyArticle"],
  "author": {
    "@embed": "@always"
  },
  "creator": {
    "@embed": "@always"
  }
}

结果:

{
  "@context": "https://schema.org/docs/jsonldcontext.json",
  "@graph": [
    {
      "type": "NewsArticle",
      "articleBody": "Article Body",
      "author": {
        "id": "_:b1",
        "type": "Person",
        "name": "Creator Name",
        "url": "https://example.org/author/creator-name/"
      },
      "creator": {
        "id": "_:b1",
        "type": "Person",
        "name": "Creator Name",
        "url": "https://example.org/author/creator-name/"
      },
      "description": "Description.",
      "headline": "Headline"
    }
  ]
}