Create Structured Markup with JSON-LD

JSON-LD is a way of annotating your web pages with structured data. It stands for JavaScript Object Notation for Linked Data. Google can provide enhanced search results based on the supplied data. In addition it describes the structure of the data on your web page.

Examples of Structured Data

  • Business
  • Person
  • Page
  • Product
  • Opening Hours
  • Event

The markup for JSON-LD is described on Schema.org. Here you can see all of the types that are available for marking up your webpage. However there is a nuget package that you can reference in your C# code to generate the JSON-LD for you.

Schema.Net

Schema.Net is a nuget package designed by RehanSaeed. It turns the Schema.org definitions into C# POCO classes.

JSON-LD Person

The example below is a LINQPad script. I have added to the query the Schema.Net nuget package.

The Person object defines who the person is and has details about me to enhance the search results. I first create a new instance of that object and then start adding properties to it. The SameAs allows google to link other social accounts to me as a person.

Once the object is populated then I call ToHtmlEscapedString() and the JSON-LD is generated. I wrap the JSON with a Script tag and it is ready to use on my website.

In order to know what objects are available please view Schema.org where you can see a list of al the structured data objects available

using System;
using Schema.NET;

void Main()
{
	var person = new Person
	{
		Name = "Jonathan Purdom",
		Email = "lotushope@hotmail.com",
		Gender = Schema.NET.GenderType.Male,
		Address = new PostalAddress {
			StreetAddress = "Harwood Road",
			AddressLocality = "West Sussex",
			AddressRegion = "Littlehampton",
			PostalCode = "BN17 7AT"
		},
		Url = new Uri("https://www.lotushope.co.uk"),
		SameAs = new OneOrMany<System.Uri>
		(
			new System.Uri("http://www.facebook.com/in/jonathanpurdom"),
			new System.Uri("http://www.linkedin.com/in/jonathanpurdom")
		)
	};

	Console.WriteLine("<script type=\"application/ld+json\">");
	Console.WriteLine(person.ToHtmlEscapedString());
	Console.WriteLine("</script>");
}

Output

Here you can see the output of the script above. You can have multiple JSON-LD blocks in the header to describe different parts of the page and data.

<script type="application/ld+json">
{"@context":"https://schema.org","@type":"Person","name":"Jonathan Purdom","sameAs":["http://www.facebook.com/in/jonathanpurdom","http://www.linkedin.com/in/jonathanpurdom"],"url":"https://www.lotushope.co.uk","address":{"@type":"PostalAddress","addressLocality":"West Sussex","addressRegion":"Littlehampton","postalCode":"BN17 7AT","streetAddress":"Harwood Road"},"email":"lotushope@hotmail.com","gender":"https://schema.org/Male"}
</script>


Article Links