Sub topic 8
Marking up reviews with schema.org JSON-LD
This chapter covers how to describe reviews and ratings in a form that search engines can read. It assumes you can write basic HTML and CSS, know what a landing or product page is, and have read the earlier chapters on reviews, ratings, and the legal limits on endorsements. Structured data does not make a page more trustworthy to people. It describes content that is already on the page, so that a machine can read it without guessing.
What structured data is and why reviews use it
Structured data is extra, machine-readable information about a page. Search engines read it to learn what the page is about and, in some cases, to show richer results. Those results can include star ratings under a link.
Schema.org is a shared vocabulary of types (such as Product, Review, and Person) and properties (such as name and ratingValue). You can write schema.org data in several syntaxes. The one Google recommends is JSON-LD, which stands for JavaScript Object Notation for Linked Data. It sits in a <script> element, separate from the visible HTML, so you can add or change it without touching the layout.
Markup has no visual effect on the page. A visitor never sees it. Its value comes from two things:
- A search engine can read the rating, the number of reviews, and the item being reviewed without scraping your layout.
- A page with valid markup may become eligible for review rich results. Eligibility is not a promise. The search engine decides whether to show them.
The building blocks of a review
JSON-LD is a JSON object with a few reserved keys:
@contextsays which vocabulary the keys come from. For schema.org it ishttps://schema.org.@typenames the kind of thing the object describes.- Other keys are properties of that type. A property's value can be a string, a number, or another object with its own
@type.
Three types do most of the work for reviews:
| Type | What it describes | Common properties |
|---|---|---|
| `Product` (or another reviewed item type) | The thing being reviewed | `name`, `image`, `description`, `aggregateRating`, `review` |
| `AggregateRating` | The summary of many ratings | `ratingValue`, `reviewCount` or `ratingCount`, `bestRating`, `worstRating` |
| `Review` | One person's review | `author`, `reviewRating`, `reviewBody`, `datePublished` |
A Review holds a Rating, and the Rating holds the actual score. A review also has an author, which is a Person or an Organization.
A worked example
This is the example from the plan. It describes a product with an average rating and one individual 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>Read it from the outside in:
- The
<script>element hastype="application/ld+json". Browsers do not run it. Search engines look for it. - The top-level object is a
Productnamed "Acme Standing Desk". aggregateRatingsays the average is 4.6 across 212 reviews.reviewholds one review by Dana Ortiz, who gave 5 out of 5.
Two details matter. bestRating defaults to 5 when you leave it out, so state it whenever your scale is different, such as 10 or 100. Also, the example gives numbers as strings. Schema.org accepts both strings and numbers, and either works for search engines.
The rule that matters most: markup must match the page
Search engine guidelines require that structured data describes content visitors can see on the same page. This is the main rule for review markup, and breaking it can cost you rich results or trigger a manual action against the site.
Apply it as a checklist:
- The reviews in the markup must also be visible on the page, or the page must show the rating and review count that the markup states.
- The numbers must agree. If the markup says 212 reviews and the page says 187, fix one of them.
- The reviews must come from real customers, as covered in the chapter on authenticity and law. You cannot mark up reviews you wrote, bought, or took from another site.
- Marking up content that is hidden from users, such as a review that exists only in the JSON, is a violation.
- The reviewed item should be the thing the page is about. A page for one product should not carry a rating for the whole company.
Self-serving reviews
Google has said it does not show review rich results for "self-serving" reviews of an organization or local business, where the business marks up reviews about itself on its own site. Reviews of products and other specific items are treated differently. Guidelines change, so check the current documentation for the type you are marking up before you rely on a particular result.
Choosing between aggregate rating and individual reviews
Use aggregateRating when your page shows a summary, such as "4.6 out of 5 from 212 reviews". Use review when the page shows full individual reviews. Many product pages show both, and the markup can too.
Some points to keep straight:
aggregateRatingneeds a rating value and a count. UsereviewCountwhen the count is of written reviews. UseratingCountwhen people could rate without writing anything.- Do not make up a count. If you have no reviews yet, leave the markup out. Do not publish
"reviewCount": "0". - If a page lists many reviews, you do not have to mark all of them up. The aggregate plus a few representative reviews is acceptable, as long as everything marked up is visible.
- Keep the average honest. A rating of 4.6 should be the real mean of the ratings that are counted, including the low ones. Filtering out negative reviews before computing the average is the kind of practice the legal chapter warns about.
Generating markup from real data
Hand-written JSON-LD goes stale. The number of reviews grows, the average moves, and the markup stays at last month's values. The safest pattern is to generate the markup from the same data that renders the visible reviews. Then the two cannot drift apart.
This Python script builds the JSON-LD from a list of review records and prints a complete <script> block. It computes the average and count itself, so the values always match the data.
import json
from statistics import mean
reviews = [
{"author": "Dana Ortiz", "rating": 5, "body": "Sturdy, quiet, and easy to assemble."},
{"author": "Sam Lee", "rating": 4, "body": "Good desk. The motor is a little slow."},
{"author": "Priya Nair", "rating": 5, "body": "Worth it for the stability alone."},
]
def build_product_markup(name, reviews, max_reviews=2):
if not reviews:
return None # No reviews yet: publish no rating markup at all.
ratings = [r["rating"] for r in reviews]
data = {
"@context": "https://schema.org",
"@type": "Product",
"name": name,
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": f"{mean(ratings):.1f}",
"reviewCount": str(len(reviews)),
"bestRating": "5",
"worstRating": "1",
},
"review": [
{
"@type": "Review",
"author": {"@type": "Person", "name": r["author"]},
"reviewRating": {
"@type": "Rating",
"ratingValue": str(r["rating"]),
"bestRating": "5",
},
"reviewBody": r["body"],
}
for r in reviews[:max_reviews]
],
}
return data
markup = build_product_markup("Acme Standing Desk", reviews)
if markup is not None:
# Escape "<" so review text can never close the script element early.
payload = json.dumps(markup, indent=2).replace("<", "\\u003c")
print('<script type="application/ld+json">')
print(payload)
print("</script>")Running it prints a Product with an average of 4.7 across 3 reviews, and the first two reviews listed. Three details in the script are deliberate:
- It returns nothing when there are no reviews, which follows the "no empty counts" rule above.
- It uses
json.dumpsinstead of string concatenation, so quotes and special characters in review text are escaped correctly. - It replaces
<with its Unicode escape. Review text comes from visitors. Without this step, a review containing</script>could end the script block early and inject markup into your page. JSON parsers read\u003cas<, so the data is unchanged.
<br>
Common errors
Most problems with review markup fall into a small set of mistakes.
| Mistake | Why it is a problem | Fix |
|---|---|---|
| Rating in markup but no reviews visible | Violates the "visible on the page" rule | Show the reviews, or remove the markup |
| Count and average do not match the page | Looks like manipulation and can lose rich results | Generate both from one data source |
| Missing `author` on a `Review` | The review is incomplete and may be ignored | Add a `Person` or `Organization` with a `name` |
| `ratingValue` outside the scale | Search engines cannot interpret it | Set `bestRating` and `worstRating` to match your scale |
| Markup on a page that lists many different products | The rating is ambiguous | Mark up each product on its own page |
| Copying reviews from a third-party site | Not your content, and may break their terms | Mark up only reviews collected on your site |
| Markup for reviews you edited to look better | Misleading and may be illegal | Publish the real review text |
Testing and monitoring
Valid syntax is not enough, so test the markup before and after you publish it.
- Paste the page URL or code into Google's Rich Results Test. It reports errors, which block eligibility, and warnings, which are optional properties you left out.
- Use the Schema Markup Validator at validator.schema.org to check against the full schema.org vocabulary, not only the types Google supports.
- After the page is live, open the Enhancements and Search Appearance reports in Google Search Console to see whether the pages are recognized and whether issues appear over time.
- Re-test after template changes. A redesign that moves or removes the visible reviews can leave the markup out of date.
Passing these tools does not guarantee stars in search results. They confirm the markup is well formed. They do not decide whether a search engine shows the result.
Where JSON-LD goes in the page
You can place the <script> block in the <head> or in the <body>. Both are valid, and search engines find it either way. Put it where your templating makes it easiest to keep next to the visible reviews. Several <script> blocks on one page are allowed. You can also use a single block that holds an array of objects, for instance a Product and an Organization.
Because JSON-LD is not rendered, it adds little to page weight for a normal product page. Keep it to the properties you need. Large blocks that repeat hundreds of reviews add bytes without adding value.
Decision guide
Use these questions in order before you add markup.
- Does the page show real reviews or a real rating from customers? If not, add no markup.
- Is the reviewed item the main subject of the page? If not, move the markup to the page where it is.
- Can the markup be generated from the same data as the visible content? If so, do that.
- Does every number in the markup appear, or follow directly, from what a visitor can see? If not, correct it.
- Have you validated it and checked it again after the next template change?
<br>
Exercises
Exercise
A product page shows a summary "4.4 average from 96 reviews" but no individual reviews. The JSON-LD includes an aggregateRating with 4.4 and 96, plus a review object for a 5-star review that does not appear anywhere on the page. What is wrong, and how do you fix it?
Show answer
The review object describes content that visitors cannot see, which breaks the rule that markup must match visible content. Either remove the hidden review and keep the aggregateRating, which matches the visible summary, or display that review on the page so the markup describes something real. Also confirm the review is a genuine customer review collected on your site.
Exercise
Your store has launched a new product that has no reviews yet. A teammate suggests publishing aggregateRating with "ratingValue": "5" and "reviewCount": "1" using a review from the founder. Should you? Give two reasons.
Show answer
No. First, a founder's review is not an independent customer review, so it is a misleading endorsement and falls under the authenticity and legal limits covered earlier. Second, a one-review rating gives a visitor almost nothing to judge by, and any rating markup must match what the page shows. The correct choice is to publish no rating markup until real customers have left reviews.
Exercise
A customer review contains the text Great desk </script><script>alert(1)</script>. Your template inserts review text into JSON-LD with plain string concatenation. What happens, and what should you do instead?
Show answer
The </script> in the review ends the JSON-LD block early, and the following <script> runs as code on your page, which is a script injection. Build the data as an object, serialize it with a JSON library so that quotes are escaped, and replace < with \u003c in the output so that visitor text can never close the element.