I am trying to access the content in eventbrite emails that I receive but the html code doesn't have an ID associated to the JSON-LD script. So is there a way to still access this data? If so, how?
Is it possible to perhaps attach a temporary id to the JSON-LD script tag so that I can access the data? If so, how?
To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.
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.
Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.
You can get all JSON-LD blocks with
document.querySelectorAll('script[type="application/ld+json"]');
or just the first one with
document.querySelector('script[type="application/ld+json"]');
Here's a full example:
var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText);
document.getElementById('result').innerText = jsonld.endDate;
<html>
<head>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Event",
"name": "A random event",
"startDate": "2013-09-14T21:30",
"endDate": "2013-09-14T21:30"
}
</script>
</head>
<body>
<p>The end date is: <strong id="result"></strong></p>
</body>
</html>
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