Warm tip: This article is reproduced from serverfault.com, please click

json-ld: Good way to model custom values

发布于 2016-12-12 16:37:07

I'm trying to get a good json-ld that combines the schema.org/Product definition with some custom elements.

I'm coming from an xsd background and the extensibility in json-ld seems very difficult to achieve.

I started from the template markup for Products found at Google (https://developers.google.com/search/docs/guides/search-gallery) and tried to extend it (I would like to add something like mydomain:tags to it) but I'm not sure how to do this.

<script type="application/ld+json">
{
  "@context": ["http://schema.org/",
    {"mydomain": "http://mystuff.com/"}],
  "@type": "Product",
  "name": "Executive Anvil",
  "image": "http://www.example.com/anvil_executive.jpg",
  "description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
  "mpn": "925872",
  "brand": {
    "@type": "Thing",
    "name": "ACME"
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "4.4",
    "reviewCount": "89"
  },
  "offers": {
    "@type": "Offer",
    "priceCurrency": "USD",
    "price": "119.99",
    "priceValidUntil": "2020-11-05",
    "itemCondition": "http://schema.org/UsedCondition",
    "availability": "http://schema.org/InStock",
    "seller": {
      "@type": "Organization",
      "name": "Executive Objects"
    }
  },
  "mydomain:tags" : {}
}
</script>

Any clue on what I'm doing wrong here would be much appreciated. It's probably something silly...

Questioner
CodingEdge
Viewed
0
unor 2016-12-18 19:06:07

Your JSON-LD seems to be correct. You are using a combination of example 19 (Compact IRIs) and example 29 (Advanced Context Usage).

Google’s Structured Data Testing Tool is not a general JSON-LD validator. The errors it reports are primarily for their search result features. Their error ("The property http://mystuff.com/tags is not recognized by Google for an object of type Product.") just says that it’s not one of the properties Google knows, which is, of course, correct.

If you want to validate your JSON-LD, without getting errors for Google-specific features, you could use http://json-ld.org/playground/, for example.