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?
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.
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With