Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding JSON-LD with JavaScript

I am trying to implement structure data using JSON-LD. What I am doing is dynamically get content using jQuery and making JSON format and appending inside the head element.

<script id="dynamicJSONLD"></script>
$(document).ready(function(){
var product_name = $(.product-pg .product-name).text();
data = {
               "@context": "https://schema.org",
               "@type": "Product",
               "url": "https://www.example.com/productone",
               "name": product_name  
            };

//creating the script element and storing the JSON-LD

var script = document.createElement('script');
script.type = "application/ld+json";
script.innerHTML = JSON.stringify(data);
document.getElementsByTagName('head')[0].appendChild(script);

//OR

//storing the JSON-LD using ID
$("#dynamicJSONLD").html(JSON.stringify(data));
});

Do both ways (creating the script element and storing the JSON-LD / storing the JSON-LD using ID) work? Which is the best implementation way? Also, does Google crawl dynamically added JSON-LD like above, using JavaScript?

like image 876
user254153 Avatar asked Jul 17 '18 08:07

user254153


People also ask

Is JSON-LD JavaScript?

JSON-LD stands for JavaScript Object Notation for Linked Data, which consists of multi-dimensional arrays (think: list of attribute-value pairs). It is an implementation format for structuring data analogous to Microdata and RDFa.

Where do I put JSON-LD?

JSON-LD structured data is a script that can be placed anywhere on a web page that communicates Schema.org structured data. It can be placed in the head section with all the other meta data like the title tag and meta description. It can also be placed near the end of the code, near the closing body tag.

What is the difference between JSON and JSON-LD?

JSON Schema is primarily used for describing large sets of self-contained data, used as a data descriptor it's similar to XML schema. JSON LD is a way to describe data use of the web, more similar to schema.org or meta descriptors. It's consumed for more than one type of object and can be used to discover schema.

Does JSON-LD have to be in head?

It is generally safe to insert JSON-LD code within the <head> of your website, as such this is what we recommend. JSON-LD can also be inserted in the <body> of a website, or anywhere else for that matter. We've confirmed this by running several structured data verification's through Google's testing tool.


1 Answers

Yes, Google can crawl dynamically added JSON-LD, as explained in this Google article on the topic:

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.

Both methods will definitely work, but if you're going to store the JSON-LD using ID you'll need to add the required type attribute to the script:

<script id="dynamicJSONLD" type="application/ld+json"></script>

Once you finish adding your markup, make sure to test it with Google's Structured Data Testing Tool!

like image 134
Noah Jeffrey Avatar answered Sep 26 '22 01:09

Noah Jeffrey