1

I have an HTML document with JSON-LD published as follows:

<script type="application/ld+json">
    {
           "@context": {
               "gs1": "http://vocab.gs1.org/v1#",
               "xsd":"http://www.w3.org/2001/XMLSchema#"
           },
           "@id": "retailer:12345",
           "@type": "gs1:Offering",
           "gtin13":"5011476100111",
           "gs1:hasPrice": {
               "gs1:price": "2.49",
               "gs1:priceTypeCode": "USD",
               "@type":"gs1:Price"
           }
      }
</script>

This works as expected (meaning it is valid and visible using Google Structured Data Testing Tool and Structured Data Linter)

Using Same Origin (not Cross Origin), how to move the in-line JSON-LD to a file?

This approach fails:

<script type="application/ld+json" src="_URL_.json"></script>

This approach also fails:

<script type="application/ld+json">
        $(document).ready(function(){
            $.ajax({
                url: "_URL_.json",
                dataType: 'json',
                });        
        });
</script>

How to correct my approach to create a solution where the JSON-LD is in file on my server, not in the HTML?

Next, assume my site is CORS-enabled.

How to publish the JSON-LD structure in a JSONP format so that another domain can subscribe to the JSON-LD data?

Jay Gray
  • 1,706
  • 2
  • 20
  • 36

2 Answers2

2

Tools like the Google Structured Data Testing Tool or the Structured Data Linter don’t support external references. And probably many consumers of your JSON-LD neither.

If you want to do it anyway, you would have to use a link instead of a script element, because for the use as data block, the script element may not have a src attribute.

The variant with including the JSON-LD dynamically might work for consumers that support JavaScript for that purpose. (Google seems to support it; maybe it’s only their testing tool that can’t use it.)

Community
  • 1
  • 1
unor
  • 92,415
  • 26
  • 211
  • 360
  • Would you show me what your `link` structure would look like? If I can make it work, I'll accept the answer. I'll move the `JSON-LD` format for `JSONP` to a new question, as I see that as an alternative; and I need to publish the `JSON-LD` as `JSONP` for other reasons. – Jay Gray Oct 20 '15 at 13:03
  • 1
    @JayGray: I don’t know any consumer that supports fetching external JSON-LD via `link`; if there’s one that supports it, they might expect a specific link type. For other RDF serializations (like RDF/XML, Turtle etc.), the `alternate` link type is often used: `` – unor Oct 20 '15 at 13:20
1

You could generate your script tag dynamically:

<script>
  // do some ajax, get the JSON-LD as `data` and then...
  $.ajax(...).done(function(data) {
    var el = document.createElement('script');
    el.type = 'application/ld+json';
    el.text = JSON.stringify(data);
    document.querySelector('body').appendChild(el);
  });
</script>
dlongley
  • 2,078
  • 14
  • 17
  • This works. However, as @unor warned, Google structure data tool does not acknowledge the `stringify(data)` as JSON-LD because it is an external reference. I need to reconsider my approach. My interim solution will be to follow @unor advice and identify the URI using `` – Jay Gray Oct 22 '15 at 11:27