<script>
function renderJSONLD(jsonData) {
try {
// Parse JSON if it comes as a string (ensure it's an object)
const parsedData = typeof jsonData === 'string' ? JSON.parse(jsonData) : jsonData;
// Convert to a JSON string for embedding
const jsonString = JSON.stringify(parsedData);
// Find or create the <script> tag for JSON-LD
let scriptTag = document.getElementById('jsonld-script');
if (!scriptTag) {
scriptTag = document.createElement('script');
scriptTag.type = 'application/ld+json';
scriptTag.id = 'jsonld-script';
document.head.appendChild(scriptTag);
}
// Update the <script> tag content
scriptTag.textContent = jsonString;
console.log('JSON-LD script tag updated successfully:', jsonString);
} catch (error) {
console.error('Failed to process JSON-LD:', error);
}
}
</script>
Below this I include the jsonLD script with id used in the above script: