Structured Data using Schema.NET

Schema.NET

What is Schema.org?

Schema.org defines a set of standard classes and their properties for objects and services in the real world. There are nearly 700 classes at the time of writing defined by schema.org. This machine readable format is a common standard used across the web for describing things.

Where is Schema.org Used?

Websites

Websites can define Structured Data in the head section of their html to enable search engines to show richer information in their search results. Here is an example of how Google can display extended metadata about your site in it's search results.

Google Logo Structured Data Example

Using structured data in html requires the use of a script tag with a MIME type of application/ld+json like so:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Organization",
  "url": "http://www.example.com",
  "name": "Unlimited Ball Bearings Corp.",
  "contactPoint": {
    "@type": "ContactPoint",
    "telephone": "+1-401-555-1212",
    "contactType": "Customer service"
  }
}
</script>

Windows UWP Sharing

Windows UWP apps let you share data using schema.org classes. Here is an example showing how to share metadata about a book.

Enter Schema.NET

Schema.NET is Schema.org objects turned into strongly typed C# POCO classes for use in .NET. All classes can be serialized into JSON/JSON-LD. Here is a simple Schema.NET example that defines the name and URL of a website:

var website = new WebSite()
{
    AlternateName = "An Alternative Name",
    Name = "Your Site Name",
    Url = new Uri("https://example.com")
};
var jsonLd = website.ToString();

The code above outputs the following JSON-LD:

{
    "@context":"http://schema.org",
    "@type":"WebSite",
    "alternateName":"An Alternative Name",
    "name":"Your Site Name",
    "url":"https://example.com"
}

There are dozens more examples based on Google's Structured Data documentation with links to the relevant page in the unit tests of the Schema.NET project.

Classes & Properties

schema.org defines classes and properties, where each property can have a single value or an array of multiple values. Additionally, properties can have multiple types e.g. an Address property could have a type of string or a type of PostalAddress which has it's own properties such as StreetAddress or PostalCode which breaks up an address into it's constituent parts.

To facilitate this Schema.NET uses some clever C# generics and implicit type conversions so that setting a single or multiple values is possible and that setting a string or PostalAddress is also possible:

// Single string address
var organization = new Organization()
{
    Address = "123 Old Kent Road E10 6RL"
};

// Multiple string addresses
var organization = new Organization()
{
    Address = new List<string>()
    { 
        "123 Old Kent Road E10 6RL",
        "456 Finsbury Park Road SW1 2JS"
    }
};

// Single PostalAddress address
var organization = new Organization()
{
    Address = new PostalAddress()
    {
        StreetAddress = "123 Old Kent Road",
        PostalCode = "E10 6RL"
    }
};

// Multiple PostalAddress addresses
var organization = new Organization()
{
    Address = new List<PostalAddress>()
    {
        new PostalAddress()
        {
            StreetAddress = "123 Old Kent Road",
            PostalCode = "E10 6RL"
        },
        new PostalAddress()
        {
            StreetAddress = "456 Finsbury Park Road",
            PostalCode = "SW1 2JS"
        }
    }
};

This magic is all carried out using the Value<T>, Value<T1, T2>, Value<T1, T2, T3> etc. types. These types are all structs for best performance too.

Where to Get It?

Download the Schema.NET NuGet package or take a look at the code on GitHub. At some point I'll find the time to write a quick ASP.NET Core tag helper that wraps Schema.NET.

Web Mentions

What's this?

0 Replies

GitHub Comments

RehanSaeed

Chad Boettcher Chad Boettcher commented on 2017-09-01 19:02:41

I'm having a problem initializing Values objects. For example, if I'm creating an Organization object, how to I initialize an Awards object with 2 awards?

var organization = new Organization()
{
    Address = new PostalAddress()
    {
        StreetAddress = this.CurrentWebsite.CustomerServiceAddress1,
        AddressLocality = this.CurrentWebsite.CustomerServiceCity,
        AddressRegion = this.CurrentWebsite.CustomerServiceState,
        PostalCode = this.CurrentWebsite.CustomerServicePostalCode,
        AddressCountry = "US"
    },
    Award = new Values()
    {
        // How do you do this for "Award 1" and "Award 2"
    },
};
RehanSaeed

Chad Boettcher Chad Boettcher commented on 2017-09-06 22:39:55

Award = new List()
{
    "Award 1",
    "Award 2"
},

By the way, you sir, are a genius. This package saved me a TON of time! Thanks!

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2017-09-12 15:20:22

I'm having a problem initializing Values objects. For example, if I'm creating an Organization object, how to I initialize an Awards object with 2 awards?

var organization = new Organization()
{
    Address = new PostalAddress()
    {
        StreetAddress = this.CurrentWebsite.CustomerServiceAddress1,
        AddressLocality = this.CurrentWebsite.CustomerServiceCity,
        AddressRegion = this.CurrentWebsite.CustomerServiceState,
        PostalCode = this.CurrentWebsite.CustomerServicePostalCode,
        AddressCountry = "US"
    },
    Award = new Values()
    {
        // How do you do this for "Award 1" and "Award 2"
    },
};

This question is better suited to a GitHub issue. Like so:

var organization = new Organization()
{
    Award = new List<string>()
    {
        "Award 1",
        "Award 2"
    },
};

You can use a List<T> or T[] array.

RehanSaeed

Steve Steve commented on 2018-05-10 04:48:46

Great work!

I have a question tho, how can we set the dateCreated to just the year of the book published?

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2018-05-10 10:43:33

Great work!

I have a question tho, how can we set the dateCreated to just the year of the book published?

I'm not sure what you mean but you can just set the property to a DateTimeOffset value.

RehanSaeed

Steve Steve commented on 2018-05-10 10:55:56

I'm not sure what you mean but you can just set the property to a DateTimeOffset value.

Normally, for our collection of books, we only have the publication year (we do not have the complete date)... How can we set the dateCreated to just the year (eg 2007)? The DateTimeOffset data type requires year, month and day.

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2018-05-10 11:01:28

Normally, for our collection of books, we only have the publication year (we do not have the complete date)... How can we set the dateCreated to just the year (eg 2007)? The DateTimeOffset data type requires year, month and day.

According to https://schema.org/dateCreated, the dateCreated type only supports date or dateTime types. So setting the year alone is not possible.

RehanSaeed

Steve Steve commented on 2018-05-10 11:19:25

I understand.

In https://schema.org/Date, it mentions that it should follow ISO8601 format. Which allows yyyy date format.

ISO 8601 prescribes, as a minimum, a four-digit year [YYYY]...
W3

RehanSaeed
(edited )

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2018-05-10 11:27:15

I understand.

In https://schema.org/Date, it mentions that it should follow ISO8601 format. Which allows yyyy date format.

ISO 8601 prescribes, as a minimum, a four-digit year [YYYY]...
W3

Yes you are right...that's an interesting problem. The only way I can think to resolve it is for every date property like dateCreated, to add a format property like dateCreatedFormat which would take a C# date format string. This could then be used in the serialization/deserialization.

Please do raise an issue in the Schema.NET GitHub site. We'll see if we can get that implemented or also feel free to submit a pull request.

RehanSaeed

Steve Steve commented on 2018-05-10 11:53:09

Yes you are right...that's an interesting problem. The only way I can think to resolve it is for every date property like dateCreated, to add a format property like dateCreatedFormat which would take a C# date format string. This could then be used in the serialization/deserialization.

Please do raise an issue in the Schema.NET GitHub page. We'll see if we can get that implemented or also feel free to submit a pull request.

Done. :)

Thanks for the quick response! Cheers!

RehanSaeed

Asad Butt Asad Butt commented on 2018-06-23 21:46:37

Thanks for this wonderful work. I'm having slight problem with adding logo to my structure as it is not part of Article object.
Any thoughts on that ?
Thanks

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2018-06-24 11:33:11

Thanks for this wonderful work. I'm having slight problem with adding logo to my structure as it is not part of Article object.
Any thoughts on that ?
Thanks

Logo is not part of the article object on schema.org.

RehanSaeed

Vikas Singh Vikas Singh commented on 2018-11-29 09:45:02

I'm having a problem initializing MedicalSpeciality objects. For example, if I'm creating an Physician object, how to I initialize a MedicalSpeciality with multiple speciality?

var physician = new Physician()
{
    Name = objModel.DoctorPersonalVM.DoctorName,
    Url = new Uri(Request.Url.GetLeftPart(UriPartial.Authority) + objModel.DoctorPersonalVM.Slug),
    Description = objModel.DoctorPersonalVM.AboutMe,
    MedicalSpecialty = new MedicalSpecialty()
    {
        objModel.DoctorPersonalVM.Speciality
    }
    Award = new List<string>() 
    {
        objModel.DoctorPersonalVM.AwardName,
    },
};
RehanSaeed
(edited )

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2018-12-01 11:00:50

I'm having a problem initializing MedicalSpeciality objects. For example, if I'm creating an Physician object, how to I initialize a MedicalSpeciality with multiple speciality?

var physician = new Physician()
{
    Name = objModel.DoctorPersonalVM.DoctorName,
    Url = new Uri(Request.Url.GetLeftPart(UriPartial.Authority) + objModel.DoctorPersonalVM.Slug),
    Description = objModel.DoctorPersonalVM.AboutMe,
    MedicalSpecialty = new MedicalSpecialty()
    {
        objModel.DoctorPersonalVM.Speciality
    }
    Award = new List<string>() 
    {
        objModel.DoctorPersonalVM.AwardName,
    },
};

Please raise a GitHub issue. This is not the right place to post issues.

RehanSaeed

Zander Zander commented on 2019-04-01 13:36:41

Great work, Rehan!

I'm a bit confused - is there a way to deserialize JSON schema into Schema.NET C# Book type (for example)?

RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2019-04-01 14:04:19

Great work, Rehan!

I'm a bit confused - is there a way to deserialize JSON schema into Schema.NET C# Book type (for example)?

You can just use standard JSON serialization for that. See unit tests.

RehanSaeed
(edited )

Asim Shahzad Asim Shahzad commented on 2019-07-17 13:19:58

Hi,

I have implement your solution. When I test it in google testing tool, tool does not show any thing to me. what will be the expected reason?

I'm using asp.net mvc.

{"@context":"http://schema.org","@type":"Organization","name":"Malik Express","sameAs":"https://www.facebook.com/malikexpress","url":"https://malikexpress.com","address":{"@type":"PostalAddress","addressCountry":"PK","addressLocality":"Islamabad","addressRegion":"ICT","postalCode":"44000","streetAddress":"Javed Plaza, A.K. Fazl-ul-Haq Rd, Block A G 6/3 Blue Area, Islamabad, Islamabad Capital Territory 44000"},"email":"[email protected]","logo":"https://malikexpress.com/Tempate/img/logo.png"}
RehanSaeed

Muhammad Rehan Saeed Muhammad Rehan Saeed commented on 2019-07-17 16:23:35

hi,

I have implement your solution. When I test it in google testing tool, tool does not show any thing to me. what will be the expected reason?

I'm using asp.net mvc.

{"@context":"http://schema.org","@type":"Organization","name":"Malik Express","sameAs":"https://www.facebook.com/malikexpress","url":"https://malikexpress.com","address":{"@type":"PostalAddress","addressCountry":"PK","addressLocality":"Islamabad","addressRegion":"ICT","postalCode":"44000","streetAddress":"Javed Plaza, A.K. Fazl-ul-Haq Rd, Block A G 6/3 Blue Area, Islamabad, Islamabad Capital Territory 44000"},"email":"[email protected]","logo":"https://malikexpress.com/Tempate/img/logo.png"}

Hard to say with this much information.

RehanSaeed

larrybud larrybud commented on 2020-01-14 01:10:19

It should be noted that to output this in razor syntax, you must use

@Html.Raw()

So the example above which says:

var jsonLd = website.ToString();

The output should be

@Html.Raw(website)

If you just do

@jsonLd

You'll get html encoded strings, which is no good.

Kk-112688

Hi Rehan,

I have a doubt regarding SiteNavigationElement. Can you please tell me where can we assign the list of SiteNavigationElement in Website object. Unable to find. Thanks.