I have the following script, my question is given after the script.
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "Bat,
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.9",
"ratingCount": "77"
},
"offers": {
"@type": "AggregateOffer",
"lowPrice": "5.76",
"highPrice": "8",
"availability": "http://schema.org/InStock",
"priceCurrency": "USD"
}
}
I want update ratingValue
and ratingCount
One easy way is to generate the whole script tag dynamically. Note that I only add the generated json to the body in order to visually present what is added to the head. You can add structured data to the body though.
const product = { "name": "MyProduct", "ratingValue": "4.9", "ratingCount": "77", "lowPrice": "5.76", "highPrice": "8" } const structuredData = { "@context": "http://schema.org/", "@type": "Product", "name": "Bat", "aggregateRating": { "@type": "AggregateRating", "ratingValue": product.ratingValue, "ratingCount": product.ratingCount }, "offers": { "@type": "AggregateOffer", "lowPrice": product.lowPrice, "highPrice": product.highPrice, "availability": "http://schema.org/InStock", "priceCurrency": "USD" } } const script = document.createElement('script'); script.setAttribute('type', 'application/ld+json'); script.textContent = JSON.stringify(structuredData); document.head.appendChild(script); const show = document.getElementById('show'); show.innerHTML = JSON.stringify(structuredData);
<.doctype html> <html lang="en"> <head> </head> <body> <.-- your content here... --> <div id="show"></div> </body> </html>
You can find some more documentation to the topic here: https://developers.google.com/search/docs/appearance/structured-data/generate-structured-data-with-javascript
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.