DEV Community

Victoria
Victoria

Posted on

The Fastest Way to Create Valid JSON-LD for SEO

Just spent the weekend building a simple schema generator that actually works

I've been helping a friend optimize their recipe blog for Google visibility, and the biggest pain point was always schema markup. Writing JSON-LD by hand is tedious, and most generators I tried either output invalid markup or miss critical properties.

So I hacked together a small CLI tool that takes a URL and auto-generates the most appropriate schema type based on content analysis.

How it works:

  • Scrapes the page for content type (article, product, recipe, local business, etc.)
  • Extracts key metadata from HTML tags
  • Generates JSON-LD with all required + recommended properties
  • Validates against Google's structured data testing tool

Here's a simplified version of the content classifier:

python
import re
from urllib.parse import urlparse

def classify_content(soup, url):
# Check for recipe patterns
if soup.find('script', type='application/ld+json'):
return 'Recipe' if 'recipe' in str(soup) else 'Article'

# Check for product indicators
if soup.find('meta', {'property': 'product:price:amount'}):
    return 'Product'

# Default to Article for blogs
if len(soup.find_all('article')) > 0:
    return 'Article'

return 'WebPage'
Enter fullscreen mode Exit fullscreen mode

The tool generates complete JSON-LD blocks like this:

{
"@context": "https://schema.org",
"@type": "Recipe",
"name": "{{title}}",
"author": {
"@type": "Person",
"name": "{{author}}"
},
"datePublished": "{{date}}",
"description": "{{excerpt}}",
"prepTime": "{{prep_time}}",
"cookTime": "{{cook_time}}",
"recipeIngredient": {{ingredients}},
"recipeInstructions": {{instructions}}
}

Biggest win: My friend's recipe posts went from taking 20 minutes of manual schema work to about 30 seconds. Google Search Console now shows rich results for 90% of their posts.

For production use, I've been experimenting with SerpSpur's schema generator which handles edge cases better (like multiple schema types on one page, or nested organizations). Their API also validates against Google's latest requirements.

https://serpspur.com/tool/schema-markup-generator-json-ld/

What schema types do you find yourself writing most often? Always looking for new patterns to automate.

python #seo #jsonld #schemarkup #automation

Top comments (0)