DEV Community

Cover image for What is JSON-LD? A Developer's Guide to Structured Data
Drazen Bebic
Drazen Bebic Subscriber

Posted on • Originally published at bebic.dev on

What is JSON-LD? A Developer's Guide to Structured Data

Today's search engines can do a lot of things, but they can't do everything. When a crawler like Googlebot visits your site, it parses the HTML to render the page, but scraping the DOM to understand context is inefficient and prone to errors.

That is where JSON-LD comes in. It is a standardized way to tell search engines about the content of your website, completely detached from how this content appears to the user.

If you are serious about SEO and getting noticed by SERPs (Search Engine Results Pages), you will come across JSON-LD and you will most definitely need to understand it and implement it.

What is JSON-LD?

JSON-LD stands for JavaScript Object Notation for Linked Data. Quite a lot of words packed into that, but don't worry. What you need to know is that It's a very lightweight Linked Data format which allows you to embed structured data using a simple <script> tag.

The main benefit of JSON-LD is that you can completely decouple the data layer from the presentation layer. Website visitors see one thing, the crawlers see something else.

This magical JSON-LD block usually lives in the <head> of your document (but it can also be found in the body) inside a specific script block:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Person",
  "name": "Drazen",
  "jobTitle": "Senior Fullstack Developer",
  "url": "https://www.bebic.dev"
}
</script>
Enter fullscreen mode Exit fullscreen mode

When Googlebot sees this page it can automatically infer who "Drazen" is, without needing to guess it by parsing an <h1> tag. The JSON-LD structured data explicitly tells it that there is a Person with this name. Cool stuff.

Why JSON-LD is the Winner for SEO

This one is easy to answer: Google. There are several ways to implement structured data (Microdata, RDFa), but JSON-LD is king because it's recommended by Google.

The main reason why you want to implement JSON-LD is Rich Results. It's important to know that structured data is not a direct ranking factor, but it allows Google to display visually enhanced results inside searches.

These enhancements are enough to give you an edge and drastically improve Click-Through Rates (CTR). Here's an example of a product information Rich Result:

Product Information Rich Result

Here you can see the price and stock status of the product directly in your Google Search (Thanks, JSON-LD!). This is very low-hanging fruit for developers: A small JSON object can significantly increase the traffic your website receives.

Core Syntax and Structure

The syntax relies on a vocabulary defined by Schema.org. Here are the key components you will encounter:

  • @context: This defines the vocabulary being used. It is almost always "https://schema.org".
  • @type: This defines the entity you are describing (e.g., Article, Product, Organization).
  • Properties: Key-value pairs specific to that type.

Nesting Objects

JSON-LD really shines once you start linking data. In this example you're not just describing a product, no no; you're describing a product that has an offer, which is then sold by an Organization!

{
  "@context": "https://schema.org",
  "@type": "Product",
  "name": "Arctic Road Cargo M",
  "description": "With its signature cargo pockets, [...]",
  "offers": {
    "@type": "Offer",
    "price": "149.95",
    "priceCurrency": "EUR",
    "availability": "https://schema.org/InStock"
  }
}
Enter fullscreen mode Exit fullscreen mode

Moving from Theory to Practice

So now that you understand the meaning and syntax of JSON-LD the next step is to programmatically generate this JSON-LD stuff using your data.

In a modern stack, you won't write these JSON-LD objects yourself. You will write a function which will generate it for you. If you are using a frontend framework like Next.js, you will likely generate and inject JSON-LD dynamically using data fetched from your backend.

In a previous blog post I have explained how JSON-LD can be implemented with React and Next.js. Check out the written guide here: Enhance Your SEO with JSON-LD: A Practical Guide.

Validation and Debugging

Once you've written the JSON-LD object you're not done. Always test your structure before deploying changes. An invalid JSON-LD object can and will invalidate the entire block, causing your Rich Results to go poof.

Use these two tools:

  • Rich Results Test (Google): Tells you if your page qualifies for rich snippets (Stars, FAQs, etc.).
  • Schema Markup Validator (Schema.org): Validates the syntax and logic of your JSON-LD, even if it doesn't trigger a specific Google feature.

Automated Testing

Manually testing is okay, but it doesn't scale. If you want to get serious about SEO, you need to treat structured data just like any other critical part of your code and test it automatically.

Google's Lighthouse has a specific audit called "Structured data is valid". You can run this programmatically in your CI pipeline using the Lighthouse CI CLI.

If you want to verify the very structure of the JSON-LD your function injects into the DOM, you can write a simple E2E test with tools like Cypress or Microsoft Playwright.

Here is a Playwright example that scrapes the script tag and parses it to ensure it is valid JSON and contains expected data:

test('Homepage has valid JSON-LD', async ({ page }) => {
  await page.goto('https://localhost:3000');

  // Select the script tag
  const jsonLdScript = await page.$('script[type="application/ld+json"]');
  const content = await jsonLdScript.textContent();

  // Parse it to ensure valid JSON
  const data = JSON.parse(content);

  // Assert critical properties exist
  expect(data['@type']).toBe('WebSite');
  expect(data.name).toBe('Bebic.dev');
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

JSON-LD is the mediator between your application's data and the search engines. It's the invisible bridge between your website's content and Google and friends. Standardized and decoupled, a developer's dream!

It allows us developers to contribute to the SEO success of a project in a way that is sensible to us. I recommend that you treat JSON-LD as a core part of your data layer and your search performance will thank you for it.

Top comments (2)

Collapse
 
sloan profile image
Sloan the DEV Moderator

We loved your post so we shared it on social.

Keep up the great work!

Collapse
 
drazenbebic profile image
Drazen Bebic

@sloan Oh that's amazing, thank you very much!