/webmasters/community?hl=en
This content is likely not relevant anymore. Try searching or browse recent questions.
Getting Javascript variable into JSON+LD structured markup
2
I'm setting up a dynamically generated rich text snippet pulling data from our retail backend system. 

A section of the code looks like this: 

      "@context": "https://www.schema.org",
      "@type": "product",
      "brand": "<%=iposProductAttribute(Producer)%>",
      "name": "<%=iposData(product.web-description)%>",
      "image": "<%=iposImageURL({product-notes.image-file.3})%>",
      "description": "<%=iposTidyParagraph(product-notes.notes)%>",
      "sku": "<%=iposData(product.stkcode)%>",
      "mpn": "<%=iposData(product.manufacturer-code)%>",

That works perfectly.  

Where I am having trouble: 

I want to add the GTIN / barcode.  However, sometimes the field for the barcode contains something that isn't a valid barcode. 

I am thus trying to make sure that the GTIN is the right length before passing it on.  This seems to work in Javascript: 

    var barcode;
    var gtinJSON = ""; 
    barcode = '<%=iposData(product.primary-barcode)%>';
  if (barcode.length == 13)
      {var gtinJSON = JSON.stringify(barcode);}

My question:  How do I get gtinJSON into my JSON+LD script?  
So, ideally, something like "gtin": gtinJSON, is added to the list of attributes.

I had thought that JSON.stringify would work, but doesn't seem to.  

Is there a way to do this that's relatively straightforward? (I'm still quite new to javascript, etc.) 

thank you!
Details
Pinned
Locked
Latest Update Latest Updates (0)
Relevant Answer Relevant Answers (0)
All Replies (5)
Relevant Answer
> I'm setting up a dynamically generated rich text snippet pulling data from our retail backend system. 
In stead of trying to solve it the frontend with javascript I suggest to adjust the backend code to return the correct gtin value
marked this as an answer
Relevant Answer
Nikolaj - Thank you.  Unfortunately, unless I'm missing something, that doesn't help.  If I use something like GTIN13 instead of just GTIN, it doesn't check the length, which is my concern.  Similarly, I've been playing around with the various things similar to the ranges in the delivery example, to get it to validate the length of the gtin. But I can't get that to work.  

HugoMe - Thanks, I wish I could. But, alas, what the backend puts where is out of my control. I don't understand why it does what it does, but it is something I can't change for the foreseeable future. 

Thanks both!
Chris
marked this as an answer
Relevant Answer
Hi All,

Thank you for the suggestions.  In the end I didn't have to resort to the PHP approach, since I was *finally* (after a month+) able to pin down a developer long enough to get an answer on how to do it through our retail system.  So, all good.
marked this as an answer
This question is locked and replying has been disabled.
Discard post? You will lose what you have written so far.
Write a reply
10 characters required
Failed to attach file, click here to try again.
Discard post?
You will lose what you have written so far.
Personal information found

We found the following personal information in your message:

This information will be visible to anyone who visits or subscribes to notifications for this post. Are you sure you want to continue?

A problem occurred. Please try again.
Create Reply
Edit Reply
This will remove the reply from the Answers section.
Notifications are off
Your notifications are currently off and you won't receive subscription updates. To turn them on, go to Notifications preferences on your Profile page.
Report abuse
Google takes abuse of its services very seriously. We're committed to dealing with such abuse according to the laws in your country of residence. When you submit a report, we'll investigate it and take the appropriate action. We'll get back to you only if we require additional details or have more information to share.

Go to the Legal Help page to request content changes for legal reasons.

Reported post for abuse
Unable to send report.
Report post
What type of post are you reporting?
Google takes abuse of its services very seriously. We're committed to dealing with such abuse according to the laws in your country of residence. When you submit a report, we'll investigate it and take the appropriate action. We'll get back to you only if we require additional details or have more information to share.

Go to the Legal Help page to request content changes for legal reasons.

Reported post for abuse
Unable to send report.
This reply is no longer available.
/webmasters/threads
//accounts.google.com/ServiceLogin
You'll receive email notifications for new posts at
Unable to delete question.
Unable to update vote.
Unable to update subscription.
You have been unsubscribed
Deleted
Unable to delete reply.
Removed from Answers
Removed from Updates
Marked as Recommended Answer
Marked as Update
Removed recommendation
Undo
Unable to update reply.
Unable to update vote.
Thank you. Your response was recorded.
Unable to undo vote.
Thank you. This reply will now display in the answers section.
Link copied
Locked
Unlocked
Unable to lock
Unable to unlock
Pinned
Unpinned
Unable to pin
Unable to unpin
Marked
Unmarked
Unable to mark
Reported as off topic
Known Issue
Fixed
Marked Fixed
Unmarked Fixed
Unable to mark fixed
Unable to unmark fixed
/profile/0
false
Search
Clear search
Close search
Google apps
Main menu
Search Help Center
true
83844
false