SEO - Structured data in JSON-LD format

How to add a script in header for a specific page? This can be useful to add structured data to a page displaying a blog post for example (Learn About Article Schema Markup | Google Search Central | Documentation | Google Developers), a recipe, a product, etc. The structured data snippet should be set in the header per page. For now, I only see that when we add a script in the header, it will be applied to all pages. Example of a structured data in the header section:

        <html>
	  <head>
	    <title>Article headline</title>
	    <script type="application/ld+json">
	    {
	      "@context": "https://schema.org",
	      "@type": "NewsArticle",
	      "headline": "Article headline",
	      "image": [
	        "https://example.com/photos/1x1/photo.jpg",
	        "https://example.com/photos/4x3/photo.jpg",
	        "https://example.com/photos/16x9/photo.jpg"
	       ],
	      "datePublished": "2015-02-05T08:00:00+08:00",
	      "dateModified": "2015-02-05T09:20:00+08:00",
	      "author": [{
	          "@type": "Person",
	          "name": "Jane Doe",
	          "url": "http://example.com/profile/janedoe123"
	        },{
	          "@type": "Person",
	          "name": "John Doe",
	          "url": "http://example.com/profile/johndoe123"
	      }]
	    }
	    </script>
	  </head>
	  <body>
	  </body>
	</html>

Hi! It’s not mandatory to add the structured data (schema) in the header.
You can read more on how to add a schema on this related topic: Schema.org for Sitejet - #8 by Lucian_Dinu

Hope this helps!

1 Like

Hi @Lucian_Dinu, thank you for your reply.

As far as I know and based on the Google Documentation, generally there are 2 ways of setting up the structured data:

  1. JSON-LD in the head tag
  2. Micodata in the body tag

And according to your answer, you are saying that I should use HTML tag element in the page body and populate it with JSON-LD code. I cannot see any Google Structured Data documentation that suggests this way. Please share your source if you found this.

Hi @AJ_Joe , the info is also in the Google Structured Data documentation.

Take a look here: Understand How Structured Data Works | Google Search Central  |  Documentation  |  Google Developers

Hope this helps!

P.S. You can also use this tool to check your Schema Schema Markup Testing Tool | Google Search Central  |  Google Developers

1 Like

Hi @Lucian_Dinu, thank you very much for pointing this out. Good to know about it.

So I would go with inserting the HTML element before the closing body tag (</body>), in other word, right after the footer but not including in the footer (as it is per page): have you implemented it this way?

But I would keep the idea of having the possibility to add a custom header code par page.

I see that you have requested a feature that may resolve this: Dedicated <head> script area. I up voted and added the request of defining head script per page.