Sub topic 8
Marking up reviews with schema.org JSON-LD
This chapter assumes you know basic HTML, can read JSON, and have already placed visible reviews and ratings on a page. Structured data does not create trust by itself. It describes reviews that visitors can already see, so search engines can read them reliably.
What structured data does
Structured data is machine-readable information added to a page. Schema.org is a shared vocabulary of types such as Product, Review, and AggregateRating. JSON-LD (JSON for Linking Data) is a format for writing that vocabulary as a JSON block inside a script tag.
Search engines may use this markup to show star ratings in results. Google decides whether to show them. Valid markup makes a rich result possible, but it does not guarantee one.
JSON-LD is usually easier to maintain than inline attributes. It sits in one block, separate from your layout, so a design change is less likely to break it.
A worked example
This block describes a product with an average rating and one review.
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Product",
"name": "Acme Standing Desk",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.6",
"reviewCount": "212"
},
"review": {
"@type": "Review",
"author": { "@type": "Person", "name": "Dana Ortiz" },
"reviewRating": { "@type": "Rating", "ratingValue": "5", "bestRating": "5" },
"reviewBody": "Sturdy, quiet, and easy to assemble."
}
}
</script>The parts work like this:
@contexttells parsers that the keys come from schema.org.@typenames what is being described. The top-level item is the product, and the ratings and review nest inside it.aggregateRatingsummarizes many reviews.ratingValueis the average, andreviewCountis how many ratings went into it.reviewdescribes one review, with itsauthor, its ownreviewRating, and thereviewBodytext.bestRatingstates the top of the scale. It defaults to 5, but stating it avoids ambiguity if you use another scale.
The visible page should show the same Dana Ortiz review, the same text, and a rating of 4.6 from 212 reviews.
How the pieces fit together
Rules that keep markup legitimate
Search engine guidelines tie markup to what visitors see. Follow these rules:
- Mark up only reviews that are visible on the same page.
- Use real reviews from real customers. Never invent or copy ratings.
- Make the numbers match. If the page says 4.6 from 212 reviews, the markup must say the same.
- Describe the item being reviewed, not your own business with self-written reviews. Google restricts star results for reviews that a site writes about itself.
- Update the markup when the data changes. A stale count is a mismatch.
Generating markup from real data
Hand-writing a rating that changes daily invites errors. Build the JSON from your review data instead. This Python example computes the average and count from a list of ratings and prints a valid block.
import json
ratings = [5, 5, 4, 5, 4, 5, 4, 5]
markup = {
"@context": "https://schema.org",
"@type": "Product",
"name": "Acme Standing Desk",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": str(round(sum(ratings) / len(ratings), 1)),
"reviewCount": str(len(ratings)),
},
}
print('<script type="application/ld+json">')
print(json.dumps(markup, indent=2))
print("</script>")Because the same list can feed both the visible rating and the markup, the two stay in sync.
Common errors
| Error | Result | Fix |
|---|---|---|
| Markup for reviews not shown on the page | Rich results removed or ignored | Show the reviews, or remove the markup |
| Missing `name` on the product | Validation error | Add the item's name |
| Missing `author` on a review | Review item not eligible | Add a `Person` or `Organization` |
| Invalid JSON, such as a trailing comma | The whole block is ignored | Run it through a JSON validator |
| Rating outside the stated scale | Warning or rejection | Set `bestRating` and `worstRating` correctly |
Testing your markup
Check the page before you publish and after each template change:
- Paste the URL or code into Google's Rich Results Test and fix any errors it lists.
- Compare the numbers in the output with the visible page.
- After release, watch the enhancements reports in Search Console for new warnings.
Passing a test confirms the syntax is valid. It does not confirm that the reviews are honest, so that check stays with you.
Exercise
A page shows three customer reviews, but the JSON-LD claims an aggregateRating of 4.9 from 1,200 reviews, a figure taken from a marketing slide. What is wrong, and what should you do?
Show answer
The markup does not match what visitors can see, and the numbers have no verifiable source, so it breaks search guidelines and may be a misleading claim. Replace the values with the real average and count computed from actual collected reviews, and make sure the page shows or links to those reviews. If the real data is only three reviews, mark up only that.
Exercise
Why is it better to generate JSON-LD from your review data than to type the rating by hand?
Show answer
The visible rating and the markup then come from one source, so they cannot drift apart when new reviews arrive. This prevents mismatches that could cost you rich results.