TransWikia.com

Linking external JSON-LD schema (from Schema.org)

Webmasters Asked on November 3, 2021

I have a schema in JSON-LD as per Schema.org. When it was inline (within <script> tags), the Google structured data testing tool recognised the schema successfully.

If I link it externally, the structured data testing tool doesn’t recognise it. For example:

<script src="json/main-schema.json" type="application/ld+json"></script>

Is this something I should be worried about?


How I had the working schema (example from Schema.org):

<script type="application/ld+json">
{ "@context" : "http://schema.org",
  "@type" : "Organization",
  "url" : "http://www.your-company-site.com",
  "contactPoint" : [
    { "@type" : "ContactPoint",
      "telephone" : "+1-401-555-1212",
  "contactType" : "customer service"
    } ] }
</script>

External schema layed out in main-schema.json, linked with:

<script src="json/main-schema.json" type="application/ld+json"></script>
{ "@context" : "http://schema.org",
  "@type" : "Organization",
  "url" : "http://www.your-company-site.com",
  "contactPoint" : [
    { "@type" : "ContactPoint",
      "telephone" : "+1-401-555-1212",
  "contactType" : "customer service"
    } ] }

4 Answers

This may, or may not help, but take a look at how Trevor Fox did it:

<script>

$.getJSON( "/your-schema-file.jsonld", function( data ) {
$( "<script/>", {
"type": "application/ld+json",
"html": JSON.stringify(data)
}).appendTo( "head" );
});

</script>

This may be your best option. He used jQuery version 3.1.1 for this solution.

Answered by James Anderson Jr. on November 3, 2021

According to the documentation:

[…] Also, Google can read JSON-LD data when it is dynamically injected into the page's contents, such as by JavaScript code or embedded widgets in your content management system.

Which suggests that, while yo cannot link external json-ld data, you can still load it dynamically and inject it to the DOM. This might be good enough for your use case, as you can load static JavaScript on your page that will then inject the JSON-LD into the DOM.

Answered by Attila O. on November 3, 2021

unor’s answer sums it up, but I also want to add:

From the spec:

Setting the [type] attribute to any other value [other than a JavaScript MIME type] means that the script is a data block, which is not processed. None of the <script> attributes (except type itself) have any effect on data blocks. Authors must use a valid MIME type that is not a JavaScript MIME type to denote data blocks.

So, in other words, if you have a <script> with a type attribute set to anything other than text/javascript (or an equivalent JavaScript MIME type), then all attributes, including src, will have no effect.

Answered by chharvey on November 3, 2021

The script element can be used for two things:

  • dynamic/classic scripts
  • data

And for data, the spec defines:

When used to include data blocks, the data must be embedded inline […]

So you may use the src attribute only for scripts, not for data.

→ As JSON-LD is data, you have to inline it.

While linking a JSON-LD file is possible with the link element, the Schema.org sponsoring search engines don’t seem to support it.

Answered by unor on November 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP