Rich Results Testing

Structured Data Testing and Rich Results

Valid JSON-LD (JSON-LD and schema.org) makes a page eligible for a rich result, but Google says it "does not guarantee" one. Templates and serving issues break markup after launch, so test at every stage: the Rich Results Test 1 while developing, the Schema Markup Validator 5,868 (https://validator.schema.org/ 5,868 ) for schema.org rules without Google's, then the URL Inspection tool and Search Console 1 's site-wide rich result reports.

The Rich Results Test (https://search.google.com/test/rich-results 1 ) accepts a URL or a code snippet and fetches as Google-InspectionTool on a smartphone, so robots.txt can block it and a local server needs a tunnel. It reads the rendered source, so JSON-LD injected by JavaScript counts, though Google warns this makes Shopping crawls "less frequent and less reliable".

Catch syntax errors in the build first. Run this script on each built page (node check-jsonld.mjs dist/kettle.html); here the page's second block has a trailing comma:

check-jsonld.mjs: failing a build on broken JSON-LDJavaScript
import { readFileSync } from 'node:fs';
const html = readFileSync(process.argv[2], 'utf8');
const re = /<script type="application\/ld\+json">([\s\S]*?)<\/script>/g;
for (const [, json] of html.matchAll(re)) {
  try {
    console.log('OK   ', JSON.parse(json)['@type']);
  } catch (err) {
    console.log('ERROR', err.message);
    process.exitCode = 1;
  }
}
Output
OK    Product
ERROR Expected double-quoted property name in JSON at position 80 (line 2 column 80)