1

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>
0

1 Answer 1

4

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"}}
    }
  ]
}
Sign up to request clarification or add additional context in comments.

2 Comments

(excellent, thanks unor!) To be 100% complete, I update your code snapshot to get a (Google tool) valid sample equivalent of myExample. cf. Gist gist.github.com/boly38/0935d8b0a83b7a2fe4254fa2a6b92113
Thanks @unor for sharing this. I was playing for hours getting a connection between an array of Reviews into a Product. It seems that (at least Googles Structured Data Test Tool) cannot understand named graphs yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.