Retrieve a JS var and use it as a value in JSON-LD

  javascript, json-ld

In my C# application I am able to retrieve most of my JSON-LD values dynamically, except for location > type.

<script type="application/ld+json">
            {
              `@context`: `https://schema.org`,
              `@type`: `Event`,
              `name`: `<%#Eval("Title")%>`,
              `description`: `<%#Eval("Summary")%>`,
              `image`: `<asp:Repeater runat="server" DataSource='<%# Eval("headerImage") %>'><ItemTemplate><%# Eval("MediaUrl")%></ItemTemplate></asp:Repeater>`,
              `startDate`: `<%#String.Format("{0:yyyy-MM-dd}", (DateTime)Eval("EventStart"))%>`,
              `endDate`: `<%#String.Format("{0:yyyy-MM-dd}", (DateTime)Eval("EventEnd"))%>`,
              `eventStatus`: `https://schema.org/EventScheduled`,
              `eventAttendanceMode`: `https://schema.org/OnlineEventAttendanceMode`,
              `location`: {
                `@type`: `????`,
                `url`: `<%#Eval("ItemDefaultUrl")%>`
              },
              `organizer`: {
                `@type`: `Organization`,
                `name`: `Company Name`,
                `url`: `https://www.example.com/`
              }
            }
        </script>

I do have access to my location > type value as a JS variable but how do I use that variable ($eventType) in my JSON-LD snippet? I tried this:

`@type`: `${eventType}`,

But that didn’t work, the snippet treated it as a string and it got outputted exactly as ${eventType}

Source: Ask Javascript Questions

LEAVE A COMMENT