CodingEdge
CodingEdge

Reputation: 143

json-ld: Good way to model custom values

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

Upvotes: 2

Views: 1738

Answers (2)

Sandhya Kumari
Sandhya Kumari

Reputation: 31

If you want to use JsonLd for your ListView and DetailView in Django then you don't need to write it for all the list items added from the admin side you only need to pass JsonLdListView in the List View class and JsonLdDetailView in DetailView class and one function in model

Step-1 In models.py write this function in the model for which you have created ListView and DetailView

@property
    def sd(self):
        return {
            "@type": 'Organization',
            "description": self.description,
            "name": self.name,

        }

*name and description is the field name from the same model

from django_json_ld.views import JsonLdDetailView, JsonLdListView

Step-2

class PortfolioListView(JsonLdListView, ListView):
   pass

Step-3

class PortfolioDetailView(JsonLdDetailView, DetailView):
    def get_structured_data(self):
        sd = super(DesignzPortfolioDetailView, 
             self).get_structured_data()

    return sd

       

Upvotes: 0

unor
unor

Reputation: 96607

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.

Upvotes: 1

Related Questions