Duplicate JSON-LD scripts in head

蓝咒 提交于 2019-12-20 05:23:30

问题


I have to inject multiple script elements for JSON-LD data into the head of my application, all pertaining to the same @type. This is due to pulling in different fields from different data source.

Will this duplication cause any problems?

<script type="application/ld+json">
    {
        "@type": "Organisation",
        "name": "John Smith"
    }
</script>

<script type="application/ld+json">
    {
        "@type": "Organisation",
        "city": "London"
    }
</script>

I'm hoping this will be translated by Google as simply:

<script type="application/ld+json">
    {
        "@type": "Organisation",
        "name": "John Smith",
        "city": "London"
    }
</script>

Is that right?


回答1:


Consumers can’t/shouldn’t assume that these JSON objects describe the same thing. (Think of a web page with information about many different organizations: it would of course be wrong to assume that they are the same organization.)

JSON-LD allows you to specify that the things described in different objects are identical: give them the same @id value.

@id takes an IRI which acts as identifier (it’s useful to provide them for many reasons).

See Node Identifiers in the JSON-LD spec.

So it could look like this (using Schema.org instead of your custom vocabulary):

<script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Organization",
        "@id": "/organizations/42#this",
        "name": "ACME"
    }
</script>

<script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "Organization",
        "@id": "/organizations/42#this",
        "description": "…"
    }
</script>

(The relative URL /organizations/42#this would represent the organization itself. It’s best practice then to provide this JSON-LD as well your information about the organization under /organizations/42.)



来源:https://stackoverflow.com/questions/45876849/duplicate-json-ld-scripts-in-head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!