Search code examples
schema.orgjson-ld

Representing repeating attributes in JSON-LD


I would like to know how to represent repeating attributes using JSON-LD (recommended by Google) and Schema.org specifications. For example, how should we represent an Article with N Comments (N > 1)?

It is allowed to have an array as comment value like my example below. Seems yes as Google testing tool likes it.

But it is allowed to use a flat @graph representation instead? How? I have to evolve a site and this representation could be easier to implement.

I imagine that both are allowed? Then how to choose?

My example:

<script type="application/ld+json">
{
  "@context" : "http:\/\/schema.org",
  "@type" : "Article",
  "url" : "https:\/\/exemple.com/article?id=1234",
  "author" :{"@type" : "Person","name" : "Didier"},
  "image" : "https:\/\/exemple.com/article.jpg",
  "mainEntityOfPage" : "https:\/\/exemple.com",
  "dateModified" : "2018-06-14T19:50:02+02:00",
  "datePublished" : "2018-06-14T19:50:02+02:00",
  "publisher" : {"@type" : "Organization","name" : "exemple.com", "logo" : {"@type" : "ImageObject", "url" : "https:\/\/exemple.com\/logo.png"}},
  "headline" : "my article",
  "text" : "blah blah",
  "commentCount" : 2,
  "comment" : [{
      "author" : {"@type" : "Person", "name" : "Didier"},
      "text" : "comment first!!",
      "dateCreated" : "2018-06-14T21:40:00+02:00"
},{
      "author" : {"@type" : "Person", "name" : "Robert"},
      "text" : "second comment",
      "dateCreated" : "2018-06-14T23:23:00+02:00"
}]
}
</script>

Solution

  • The most simple (and most supported) way is to provide an array as value, like in your snippet:

    "comment": [
      {"@type": "Comment"},
      {"@type": "Comment"}
    ]
    

    If you want to use multiple top-level items (with @graph or other options), you need a way to convey that these top-level Comment items are comments for an Article.

    With @id, you can give each item a URI and reference this URI as property value instead of nesting the item:

    {
      "@context": "http://schema.org",
      "@graph": [
        {
          "@type": "Article",
          "@id": "/articles/foobar",
          "comment": [
            {"@id": "/articles/foobar#comment-1"},
            {"@id": "/articles/foobar#comment-2"}
          ]
        },
        {
          "@type": "Comment",
          "@id": "/articles/foobar#comment-1"
        },
        {
          "@type": "Comment",
          "@id": "/articles/foobar#comment-2"
        }
      ]
    }
    

    Instead of listing the comment URIs under Article, you could also refer to the Article within each Comment, with @reverse:

    {
      "@context": "http://schema.org",
      "@graph": [
        {
          "@type": "Article",
          "@id": "/articles/foobar"
        },
        {
          "@type": "Comment",
          "@id": "/articles/foobar#comment-1",
          "@reverse": {"comment": {"@id": "/articles/foobar"}}
        },
        {
          "@type": "Comment",
          "@id": "/articles/foobar#comment-2",
          "@reverse": {"comment": {"@id": "/articles/foobar"}}
        }
      ]
    }