Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structured Data JSON ld in asp c#

Tags:

json

c#

I am looking to add structure data script via json ld to a c# asp.net web application.

Here is the structured data script I am looking to add. I am getting runtime errors because of the @ conflict with c#: The name 'context' does not exist in the current context...this is obviously due to the @ syntax conflict.

What is an easy way to get around this?

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "LegalService",
"image": "https://example.com.png",
"@id": "https://example.com",
"name": "Example Name",
"description": "Example description.",
"address": {
"@type": "PostalAddress",
"streetAddress": "Example Address",
"addressLocality": "Example City",
"addressRegion": "Example State",
"postalCode": "Example Postal",
"addressCountry": "Example Country"
},
"url": "https://example.com",
"telephone": "Example number",
"sameAs": [
"example social account 1",
"example social account 2"
]
}
</script>

So it looks like the way around this, thanks to @scartag, is to add a double @.

I am also trying to add a loop inside the script, but I'm getting runtime errors. I'm not sure if this is even possible. It is for blog articles that will be generated in the CMS.

See below:

<script type="application/ld+json">
{
  "@@context": "http://schema.org",
  "@@type": "NewsArticle",
  "mainEntityOfPage": {
    "@@type": "WebPage",
    "@@id": "https://google.com/article"
  },
  @foreach (var post in recent)
    {
  "headline": "Article headline",
  "image": {
    "@@type": "ImageObject",
    "url": "https://google.com/thumbnail1.jpg",
    "height": 800,
    "width": 800
  },
  "datePublished": "2015-02-05T08:00:00+08:00",
  "dateModified": "2015-02-05T09:20:00+08:00",
  "author": {
    "@@type": "Person",
    "name": "John Doe"
  },
   "publisher": {
    "@@type": "Organization",
    "name": "Google",
    "logo": {
      "@@type": "ImageObject",
      "url": "https://google.com/logo.jpg",
      "width": 600,
      "height": 60
    }
  },
  "description": "A most wonderful article"
}
}
</script>
like image 660
Bobi Avatar asked Apr 25 '17 23:04

Bobi


People also ask

What is structured data JSON-LD?

JSON-LD structured data is a script that can be placed anywhere on a web page that communicates Schema.org structured data. It can be placed in the head section with all the other meta data like the title tag and meta description. It can also be placed near the end of the code, near the closing body tag.

Is JSON structured data?

JSON is a widely used format that allows for semi-structured data, because it does not require a schema. This offers you added flexibility to store and query data that doesn't always adhere to fixed schemas and data types.

What is the difference between microdata and JSON-LD?

There are two widely used formats for structured data: microdata and JSON-LD. With JSON-LD, a JavaScript object is inserted into the HTML of your page to define data, whereas microdata uses HTML tags and attributes to define data.

What is JSON-LD context?

JSON-LD provides JSON-LD contexts which enable JSON documents to use simple, locally-meaningful names, while still enabling the messages to be meaningful beyond the initial exchange partners.


1 Answers

After fixing the json (removing the redundant ]) as pointed in the comments.

You can escape the @ sign

"@@context": "http://schema.org",
like image 58
scartag Avatar answered Sep 28 '22 13:09

scartag