Absolutely! I can help you create a dynamic Product schema generator. Here are examples in popular languages that generate JSON-LD dynamically using product data. Please specify your environment if you want a specific language or framework.
1. JavaScript (Node.js / Frontend example)
Suppose you have a product object like this:
const product = {
name: "Wireless Headphones",
images: [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg"
],
description: "High quality wireless headphones with noise cancellation.",
sku: "WH-1234",
mpn: "MPN-5678",
brand: "SoundTech",
url: "https://example.com/products/wireless-headphones",
priceCurrency: "USD",
price: "99.99",
priceValidUntil: "2024-12-31",
availability: "InStock",
sellerName: "TechStore",
aggregateRating: {
ratingValue: 4.7,
reviewCount: 150
},
reviews: [
{
author: "John Smith",
datePublished: "2024-01-15",
reviewBody: "Fantastic sound quality and comfortable fit.",
title: "Great headphones",
ratingValue: 5
},
{
author: "Alice Jones",
datePublished: "2024-02-20",
reviewBody: "Good value for the money.",
title: "Satisfied",
ratingValue: 4
}
]
};
Function to generate schema:
function generateProductSchema(product) {
return {
"@context": "https://schema.org/",
"@type": "Product",
"name": product.name,
"image": product.images,
"description": product.description,
"sku": product.sku,
"mpn": product.mpn,
"brand": {
"@type": "Brand",
"name": product.brand
},
"offers": {
"@type": "Offer",
"url": product.url,
"priceCurrency": product.priceCurrency,
"price": product.price,
"priceValidUntil": product.priceValidUntil,
"itemCondition": "https://schema.org/NewCondition",
"availability": `https://schema.org/${product.availability}`,
"seller": {
"@type": "Organization",
"name": product.sellerName
}
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": product.aggregateRating.ratingValue.toString(),
"reviewCount": product.aggregateRating.reviewCount.toString()
},
"review": product.reviews.map(review => ({
"@type": "Review",
"author": {
"@type": "Person",
"name": review.author
},
"datePublished": review.datePublished,
"reviewBody": review.reviewBody,
"name": review.title,
"reviewRating": {
"@type": "Rating",
"ratingValue": review.ratingValue.toString(),
"bestRating": "5"
}
}))
};
}
// Usage example (e.g. render as JSON-LD script in your page)
const productSchema = generateProductSchema(product);
console.log(JSON.stringify(productSchema, null, 2));
You can embed the generated string in your HTML:
<script type="application/ld+json">
<!-- Insert output of JSON.stringify(productSchema) here -->
</script>
2. Python Example
If you have product data in Python dicts, you can similarly generate JSON-LD:
import json
def generate_product_schema(product):
schema = {
"@context": "https://schema.org/",
"@type": "Product",
"name": product["name"],
"image": product["images"],
"description": product["description"],
"sku": product["sku"],
"mpn": product["mpn"],
"brand": {
"@type": "Brand",
"name": product["brand"]
},
"offers": {
"@type": "Offer",
"url": product["url"],
"priceCurrency": product["priceCurrency"],
"price": product["price"],
"priceValidUntil": product["priceValidUntil"],
"itemCondition": "https://schema.org/NewCondition",
"availability": f"https://schema.org/{product['availability']}",
"seller": {
"@type": "Organization",
"name": product["sellerName"]
}
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": str(product["aggregateRating"]["ratingValue"]),
"reviewCount": str(product["aggregateRating"]["reviewCount"]),
},
"review": [
{
"@type": "Review",
"author": {
"@type": "Person",
"name": review["author"]
},
"datePublished": review["datePublished"],
"reviewBody": review["reviewBody"],
"name": review["title"],
"reviewRating": {
"@type": "Rating",
"ratingValue": str(review["ratingValue"]),
"bestRating": "5"
}
}
for review in product["reviews"]
]
}
return json.dumps(schema, indent=2)
# Example product dict (similar to the JS example)
product = {
"name": "Wireless Headphones",
"images": [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg"
],
"description": "High quality wireless headphones with noise cancellation.",
"sku": "WH-1234",
"mpn": "MPN-5678",
"brand": "SoundTech",
"url": "https://example.com/products/wireless-headphones",
"priceCurrency": "USD",
"price": "99.99",
"priceValidUntil": "2024-12-31",
"availability": "InStock",
"sellerName": "TechStore",
"aggregateRating": {
"ratingValue": 4.7,
"reviewCount": 150
},
"reviews": [
{
"author": "John Smith",
"datePublished": "2024-01-15",
"reviewBody": "Fantastic sound quality and comfortable fit.",
"title": "Great headphones",
"ratingValue": 5
},
{
"author": "Alice Jones",
"datePublished": "2024-02-20",
"reviewBody": "Good value for the money.",
"title": "Satisfied",
"ratingValue": 4
}
]
}
print(generate_product_schema(product))
3. PHP Example
<?php
function generateProductSchema($product) {
$schema = [
"@context" => "https://schema.org/",
"@type" => "Product",
"name" => $product["name"],
"image" => $product["images"],
"description" => $product["description"],
"sku" => $product["sku"],
"mpn" => $product["mpn"],
"brand" => [
"@type" => "Brand",
"name" => $product["brand"]
],
"offers" => [
"@type" => "Offer",
"url" => $product["url"],
"priceCurrency" => $product["priceCurrency"],
"price" => $product["price"],
"priceValidUntil" => $product["priceValidUntil"],
"itemCondition" => "https://schema.org/NewCondition",
"availability" => "https://schema.org/" . $product["availability"],
"seller" => [
"@type" => "Organization",
"name" => $product["sellerName"]
]
],
"aggregateRating" => [
"@type" => "AggregateRating",
"ratingValue" => strval($product["aggregateRating"]["ratingValue"]),
"reviewCount" => strval($product["aggregateRating"]["reviewCount"])
],
"review" => array_map(function($review) {
return [
"@type" => "Review",
"author" => [
"@type" => "Person",
"name" => $review["author"]
],
"datePublished" => $review["datePublished"],
"reviewBody" => $review["reviewBody"],
"name" => $review["title"],
"reviewRating" => [
"@type" => "Rating",
"ratingValue" => strval($review["ratingValue"]),
"bestRating" => "5"
]
];
}, $product["reviews"])
];
return json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
// Sample product
$product = [
"name" => "Wireless Headphones",
"images" => [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg"
],
"description" => "High quality wireless headphones with noise cancellation.",
"sku" => "WH-1234",
"mpn" => "MPN-5678",
"brand" => "SoundTech",
"url" => "https://example.com/products/wireless-headphones",
"priceCurrency" => "USD",
"price" => "99.99",
"priceValidUntil" => "2024-12-31",
"availability" => "InStock",
"sellerName" => "TechStore",
"aggregateRating" => [
"ratingValue" => 4.7,
"reviewCount" => 150
],
"reviews" => [
[
"author" => "John Smith",
"datePublished" => "2024-01-15",
"reviewBody" => "Fantastic sound quality and comfortable fit.",
"title" => "Great headphones",
"ratingValue" => 5
],
[
"author" => "Alice Jones",
"datePublished" => "2024-02-20",
"reviewBody" => "Good value for the money.",
"title" => "Satisfied",
"ratingValue" => 4
]
]
];
echo generateProductSchema($product);
Summary
- Prepare your product data in an object/dict/array.
- Use one of these functions to convert the data to structured schema.org JSON-LD markup.
- Inject the generated JSON-LD script into your product pages dynamically.
If you want me to generate a specific snippet for your platform or CMS (like Shopify, WordPress, etc.) or API, feel free to share your details!