Custom JSON-LD for an enterprise healthcare client
On this page
A large hospital system I work on at the agency has thousands of provider profile pages, a page for every clinic and hospital, and a sister site for its children's hospital that shows many of the same doctors. Google reads structured data off those pages to decide what a doctor is, where they practice and whether they take new patients. For years that structured data was not ours.
What was there before
A provider page in early 2023 loaded a script from the listings vendor's "knowledge tags" service, with the account ID and the entry ID in the URL, and that script injected whatever JSON-LD the vendor had for that entity. Providers who were not in the listings platform fell through to a fallback: a small JavaScript component made an AJAX call to a Craft action, which fetched the provider's Algolia search record and assembled a JSON-LD object from it, then appended a <script type="application/ld+json"> to the head. The fallback typed every doctor as ['MedicalBusiness', 'MedicalOrganization'], because that was what the vendor's listing data looked like.
So the page described a listing, not a person. The data was whatever the listings platform held, which could lag the CMS by days between syncs. The markup only existed after JavaScript ran. And there were two generators with two shapes and no test for either. SEOmatic was installed and did a fine job of the generic pages, but it knows about entries and fields, not about which of a provider's four location relations is the primary one or what "Accepting New Patients (Referral Required)" means.
Building it by hand
In May and June 2023 I added spatie/schema-org and a Provider service to the site's existing schema module. The template change was the whole point:
{{ craft.schema.getProviderJsonLd(entry)|raw }}
Server-rendered, in the cached page, one line. The service is a builder chain. As it stands today, trimmed, with the organization details replaced:
return SpatieSchema::physician()
->name($provider->name()->fullWithTitles)
->url($provider->canonicalUrl())
->telephone($provider->phone)
->description($this->_description($provider) ?? '')
->image($this->_image($provider) ?? '')
->parentOrganization(
$provider->isEmployed() ? SpatieSchema::medicalOrganization()
->name('...')
->url('https://example.org')
->sameAs([/* social profiles, Wikidata */]) : '')
->address($this->_locations($provider))
->aggregateRating($this->_aggregateRating($provider) ?? '')
->isAcceptingNewPatients($this->_acceptsNewPatientsSchema($provider))
->subjectOf($this->_subjectOf($provider) ?? '')
->medicalSpecialty(array_map(
fn ($name) => SpatieSchema::medicalSpecialty()->name($name),
$provider->specialties()
))
->knowsLanguage(/* ... */)
->hospitalAffiliation(/* ... */)
->availableService($this->_avaliableServices($provider) ?? '')
->knowsAbout([
...$this->_schemaConditions($provider),
...$this->_clinicalTrials($provider),
]);
Every private method is where the site's actual rules live, and that is the answer to "why not a plugin". _locations() takes the primary location's building address plus any secondary locations, and if the provider has no CMS locations at all it falls back to the addresses in the upstream provider data feed. _acceptsNewPatientsSchema() reads the referral types from that feed: "Hospital-Based Provider Only" is false, three different "Accepting New Patients" variants are true, therapists are always false. _aggregateRating() only exists for employed providers and only when the patient-survey vendor has a star rating. _clinicalTrials() looks up trials by the provider's NPI in a third-party trials service and caches the top three for an hour. Independent providers get no parentOrganization and no description, because the system does not employ them and should not claim them. None of that is a field a plugin can be pointed at.
The output on a real page comes out in this shape:
{
"@context": "https://schema.org",
"@type": "Physician",
"name": "Jane Doe, MD",
"url": "https://example.org/providers/jane-doe",
"telephone": "555.555.0100",
"image": { "@type": "ImageObject", "url": "https://cdn.example.org/..." },
"parentOrganization": { "@type": "MedicalOrganization", "name": "..." },
"address": [
{
"@type": "PostalAddress",
"name": "Example Medical Center",
"streetAddress": "100 Main St, Suite 200",
"addressLocality": "Springfield",
"addressRegion": "IL",
"postalCode": "62701"
}
],
"aggregateRating": { "@type": "AggregateRating", "ratingValue": 4.8, "reviewCount": 212, "bestRating": 5, "worstRating": 1 },
"isAcceptingNewPatients": "http://schema.org/True",
"medicalSpecialty": [{ "@type": "MedicalSpecialty", "name": "Cardiology" }],
"availableService": [{ "@type": "MedicalProcedure", "name": "Echocardiogram", "url": "..." }],
"knowsAbout": [
{ "@type": "MedicalCondition", "name": "Atrial Fibrillation", "url": "..." },
{ "@type": "MedicalStudy", "name": "...", "url": "..." }
],
"subjectOf": [
{ "@type": "VideoObject", "contentUrl": "...", "name": "..." },
{ "@type": "FAQPage", "mainEntity": [{ "@type": "Question", "name": "Medical School", "acceptedAnswer": { "@type": "Answer", "text": "..." } }] }
]
}
Education started out as hasCredential and moved to a FAQPage of "Residency: ..." pairs under subjectOf in September 2023, and isAcceptingNewPatients is the schema.org Boolean enumeration URL rather than true, which is what the vocabulary actually specifies.
Tests, then a slow rollout
The same PR added a Pest suite using craft-pest factories: 21 tests and 759 lines then, 26 tests and nearly a thousand lines now. Each one builds a provider with specific field values and asserts on the array the builder returns. "Disabled conditions should not be included in knowsAbout." "Fax number only when the fax field is set." "parentOrganization for therapists." I would not want to maintain a builder with this many branches without them.
I did not trust changes to it enough to ship them to every page at once. In June 2024, when I reworked the block from IndividualPhysician back to Physician and added hospital affiliations, the new version went out only to providers whose entry ID ends in 9, roughly ten percent, with the old markup for everyone else. Three weeks later it went to all of them. In March 2025 I wrapped every builder in a try/catch that logs and returns null, because a broken relation in a schema helper should drop the block, not 500 the profile page.
Locations followed in March 2024 with Hospital and MedicalClinic types, openingHoursSpecification built from the hours matrix, the FAQ module block emitted as a FAQPage, and upcoming events for the location pulled from Algolia. Over 2024 and 2025 the same module grew services for the home page, the about page, FAQ and contact pages, press releases, events, the blog and breadcrumbs. Each one is a short class and a conditional in the Twig variable.
One generator, two sites
The children's hospital site has no provider entries of its own. Its doctor pages read a shared Algolia index and fetch each profile as JSON from the main site. In April 2024 I added a sibling method to the provider service that swaps in the children's hospital as parentOrganization (and, since January 2025, filters the trials to pediatric ones), and put its output into the profile feed. The sister site's template is just <script type="application/ld+json">{{ provider.jsonLd | raw }}</script>. Locations work the same way through an anonymous action, cached for four hours. One codebase owns the rules, and each site gets markup that names the right organization.
The short version
Hand-build structured data when the page is composed from systems a plugin cannot see: an upstream provider feed, a ratings vendor, a trials registry, a search index, and business rules about who counts as employed. Use a typed builder so the vocabulary is checked at write time, render it on the server, test the branches, and roll it out by ID suffix before you trust it everywhere. Keep the plugin for the pages that are just pages.