How to create a data-element based on JSON-ld data layer? | Community
Skip to main content
Level 3
May 2, 2017

How to create a data-element based on JSON-ld data layer?

  • May 2, 2017
  • 1 reply
  • 9294 views

Hello all, 

We are currently using JSON-LD Schema on one of our sites and I am looking to create a data element and not sure how to do the data mapping. I usually use the w3c standard but for this client they have Json-LD Schema implemented on the site.

For mapping i am use to using this format digitalData.page.pageinfo but not sure how to use the script objects in Json-ld.

e.g based on the below example how do i create a data element for @type: product or "sku" or event offers[array]

i am using var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText); to display the script on the console (for testing purposes)

document.getElementsByTagName('script') also returns script object on the console but using document.getElementsByTagName('script').getAttribute('type'); or   document.querySelector(jsonld["@type"]); returns null. so I need help in capturing these object in a data element.. i need something similar to  digitalData.page.pageinfo.attribute to capture an element.

<script type="application/ld+json">{"@context": "http://schema.org","@type": "Product","sku": "305FS-UJNA5","offers": [{"@type": "Offer","price": "99.00","availability": "InStock","priceCurrency": "USD"}]}</script>
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Amit_Kumar
Level 10
May 4, 2017

Hi KeeMac,

 

It's quite simple actually, follow the following steps and it will work for you.

1. Add one id element to the JSON-LD to make it easy i.e  id="datatestId1"

<script id="datatestId1" type="application/ld+json"> { "@context": "http://schema.org", "@type": "Product", "sku": "305FS-UJNA5", "offers": [{ "@type": "Offer", "price": "99.00", "availability": "InStock", "priceCurrency": "USD" }] } </script>
var data = $("#datatestId1").html(); // parse the contents as a JSON object var json = JSON.parse(data); // get the value of offers and type from JSON object console.log(json["offers"][0]["@type"]);

Hope this helps,

Regards,

Amit

KeeMacAuthor
Level 3
May 11, 2017

Thank Amit