An image of a green fedora hat, which serves as the logo for this site.Gordy's Discourse

Using schema @IDs

Gordon Ansell How-To, Web Development, Tech 333 words.

I’ve recently been reviewing the schema I use on this blog and one of the things I wanted to do was remove repetitive definitions.

I used to have something along the lines of:

  {
    "@context": "https://schema.org",
    "@type": “Review”,
    "name": "Review of whatever.",
    "author": {
      "@type": "Person",
      "name": "Gordon Ansell",
      "url": "https://gordonansell.com"
  },
  {
    "@context": "https://schema.org",
    "@type": “BlogPosting”,
    "name": "My stunning blog post.",
    "author": {
      "@type": "Person",
      "name": "Gordon Ansell",
      "url": "https://gordonansell.com"
  }

You’ll note that in the above code I have the same data repeated for 'author' in both the Review and the BlogPosting constructs. This is the sort of repetitiveness I wanted to avoid.

I was aware of schema IDs but didn’t pursue their use when I initially added schema to this blog. I’ve now got around to doing so and I use a lot of IDs in my current schema.

It wasn’t immediately obvious to me how to use IDs, though, so I thought I’d write a quick blog article about it in case anyone else needs to know.

It’s best demonstrated by example, so instead of the code above I now use the following:

  {
    "@context": "https://schema.org",
    "@type": "Person",
    "@id": "https://gordonansell.com/#author",
    "name": "Gordon Ansell",
    "url": "https://gordonansell.com"
  },
  {
    "@context": "https://schema.org",
    "@type": “Review”,
    "name": "Review of whatever.",
    "author": { "@id": "https://gordonansell.com/#author" }
  },
  {
    "@context": "https://schema.org",
    "@type": “BlogPosting”,
    "name": "My stunning blog post.",
    "author": { "@id": "https://gordonansell.com/#author" }
  }

I define the ‘author’ separately via a Person construct with its own @id value of https://gordonansell.com/#author.

ID values should be unique within the schema definitions you set up for the particular page in question, but note that the URL-like IDs I use do not have to be working links; they're just used as a reference value for the schema.

I then refer to that ID in the Review and BlogPosting constructs, thus meaning I only have to define the author once.

In the context of my example I haven’t saved much but it comes into its own where you have long constructs that are regularly repeated.

I hope you find this useful.