Skip to content

Example: JSON-LD @graph ​

A news page as WordPress with Yoast SEO renders it: one JSON-LD block with a @graph of six nodes (organization, website, image, web page, the article, a person) linked by @id, a second block with breadcrumbs, and Open Graph tags whose title and image differ from the JSON-LD ones.

The page ​

html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rail strike: what changes for commuters on Monday - The Morning Post</title>
<meta name="description" content="Trains between Žilina and Košice stop for 24 hours; replacement buses run every hour.">
<meta property="og:type" content="article">
<meta property="og:title" content="Rail strike on Monday: what commuters need to know">
<meta property="og:site_name" content="The Morning Post">
<meta property="og:image" content="https://morningpost.example/img/strike-og.jpg">
<!-- WordPress + Yoast style: one block with a @graph, nodes linked by @id -->
<script type="application/ld+json" class="yoast-schema-graph">
{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Organization",
      "@id": "https://morningpost.example/#organization",
      "name": "The Morning Post",
      "url": "https://morningpost.example/",
      "logo": {"@type": "ImageObject", "url": "https://morningpost.example/logo.png"}
    },
    {
      "@type": "WebSite",
      "@id": "https://morningpost.example/#website",
      "url": "https://morningpost.example/",
      "name": "The Morning Post",
      "description": "News from Slovakia every morning"
    },
    {
      "@type": "ImageObject",
      "@id": "https://morningpost.example/2026/11/rail-strike/#primaryimage",
      "url": "https://morningpost.example/img/strike-1600.jpg",
      "width": 1600,
      "height": 900
    },
    {
      "@type": "WebPage",
      "@id": "https://morningpost.example/2026/11/rail-strike/",
      "url": "https://morningpost.example/2026/11/rail-strike/",
      "name": "Rail strike: what changes for commuters on Monday - The Morning Post",
      "isPartOf": {"@id": "https://morningpost.example/#website"}
    },
    {
      "@type": ["NewsArticle", "Article"],
      "@id": "https://morningpost.example/2026/11/rail-strike/#article",
      "headline": "Rail strike: what changes for commuters on Monday",
      "description": "Trains between Žilina and Košice stop for 24 hours from 6 am on Monday.",
      "url": "https://morningpost.example/2026/11/rail-strike/",
      "image": {"@id": "https://morningpost.example/2026/11/rail-strike/#primaryimage"},
      "datePublished": "2026-11-22T17:05:00+01:00",
      "dateModified": "2026-11-23T07:40:00+01:00",
      "author": [
        {"@id": "https://morningpost.example/#/schema/person/peter"},
        {"@type": "Person", "name": "Eva Horváthová"}
      ],
      "articleSection": ["Slovakia", "Transport"],
      "about": [
        {"@type": "Thing", "@id": "https://morningpost.example/tag/strikes/", "name": "Strikes"},
        {"@type": "Thing", "@id": "https://morningpost.example/tag/railways/", "name": "Railways"}
      ],
      "publisher": {"@id": "https://morningpost.example/#organization"}
    },
    {
      "@type": "Person",
      "@id": "https://morningpost.example/#/schema/person/peter",
      "name": "Peter Novák",
      "url": "https://morningpost.example/author/peter/"
    }
  ]
}
</script>
<!-- a second block: ignored, BreadcrumbList does not describe the page -->
<script type="application/ld+json">
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [
  {"@type": "ListItem", "position": 1, "name": "Home", "item": "https://morningpost.example/"},
  {"@type": "ListItem", "position": 2, "name": "Slovakia", "item": "https://morningpost.example/slovakia/"}
]}
</script>
</head>
<body><h1>Rail strike: what changes for commuters on Monday</h1></body>
</html>

The code ​

php
<?php

declare(strict_types=1);

// Reads the schema.org JSON-LD of a WordPress/Yoast style article (a @graph with @id references)
// and shows how the order of the parsers decides which value wins.

require __DIR__ . '/vendor/autoload.php';

use Tomaj\Scraper\Meta;
use Tomaj\Scraper\Parser\OgDomParser;
use Tomaj\Scraper\Parser\SchemaParser;
use Tomaj\Scraper\Scraper;

$base = $argv[1] ?? 'http://127.0.0.1:8402';

$scraper = new Scraper(['timeout' => 5]);

// 1. JSON-LD only. SchemaParser picks the NewsArticle node (not the Organization, WebSite,
//    ImageObject or WebPage nodes) and resolves the {"@id": ...} references of author and image.
$meta = $scraper->parseUrl($base . '/news.html', [new SchemaParser()]);

echo "== SchemaParser\n";
printf("%-12s %s\n", 'title', $meta->getTitle());
printf("%-12s %s\n", 'description', $meta->getDescription());
printf("%-12s %s\n", 'url', $meta->getOgUrl());
printf("%-12s %s\n", 'image', $meta->getOgImage());
printf("%-12s %s\n", 'published', $meta->getPublishedTime()?->format(DATE_ATOM));
printf("%-12s %s\n", 'modified', $meta->getModifiedTime()?->format(DATE_ATOM));
foreach ($meta->getAuthors() as $author) {
    printf("%-12s %s (%s)\n", 'author', $author->name(), $author->id() ?? 'no @id');
}
foreach ($meta->getSections() as $section) {
    printf("%-12s %s\n", 'section', $section->name());
}
foreach ($meta->getTags() as $tag) {
    printf("%-12s %s (%s)\n", 'tag', $tag->name(), $tag->id());
}
// Open Graph fields that JSON-LD has no equivalent for stay empty
printf("%-12s %s\n", 'og:title', $meta->getOgTitle() ?? '(null)');
printf("%-12s %s\n", 'site name', $meta->getOgSiteName() ?? '(null)');

// 2. Both parsers. The first one that finds a value wins, the second fills the gaps.
$html = $scraper->getBody();
$fields = static fn(Meta $meta): array => [
    'title' => $meta->getTitle(),
    'description' => $meta->getDescription(),
    'og:title' => $meta->getOgTitle(),
    'og:image' => $meta->getOgImage(),
    'site name' => $meta->getOgSiteName(),
    'authors' => implode(', ', array_map(static fn($author): ?string => $author->name(), $meta->getAuthors())),
];

foreach (['SchemaParser, OgDomParser' => [new SchemaParser(), new OgDomParser()], 'OgDomParser, SchemaParser' => [new OgDomParser(), new SchemaParser()]] as $order => $parsers) {
    echo "\n== {$order}\n";
    foreach ($fields($scraper->parse($html, $parsers)) as $name => $value) {
        printf("%-12s %s\n", $name, $value);
    }
}

Output ​

text
== SchemaParser
title        Rail strike: what changes for commuters on Monday
description  Trains between Žilina and Košice stop for 24 hours from 6 am on Monday.
url          https://morningpost.example/2026/11/rail-strike/
image        https://morningpost.example/img/strike-1600.jpg
published    2026-11-22T17:05:00+01:00
modified     2026-11-23T07:40:00+01:00
author       Peter Novák (https://morningpost.example/#/schema/person/peter)
author       Eva Horváthová (no @id)
section      Slovakia
section      Transport
tag          Strikes (https://morningpost.example/tag/strikes/)
tag          Railways (https://morningpost.example/tag/railways/)
og:title     (null)
site name    (null)

== SchemaParser, OgDomParser
title        Rail strike: what changes for commuters on Monday
description  Trains between Žilina and Košice stop for 24 hours from 6 am on Monday.
og:title     Rail strike on Monday: what commuters need to know
og:image     https://morningpost.example/img/strike-1600.jpg
site name    The Morning Post
authors      Peter Novák, Eva Horváthová

== OgDomParser, SchemaParser
title        Rail strike: what changes for commuters on Monday - The Morning Post
description  Trains between Žilina and Košice stop for 24 hours; replacement buses run every hour.
og:title     Rail strike on Monday: what commuters need to know
og:image     https://morningpost.example/img/strike-og.jpg
site name    The Morning Post
authors      Peter Novák, Eva Horváthová

What to notice ​

  • The right node. SchemaParser uses the NewsArticle (its @type is a list, ["NewsArticle", "Article"]) and ignores the Organization, WebSite, ImageObject, WebPage and BreadcrumbList nodes, even though they come first and have their own url, name and description. See Which node is used.
  • References resolved. The first author is only {"@id": ".../person/peter"} in the article; the parser finds the Person node and returns its name with the @id. The second author is inline and has no @id. The image is a reference to the ImageObject node.
  • No Open Graph from JSON-LD. og:title and the site name stay null with SchemaParser alone.
  • The order decides. With SchemaParser first, the title is the clean headline and the image is the 1600 px one from JSON-LD. With OgDomParser first, you get the <title> with the site suffix, the meta description and the social image. The authors are the same either way: the page has no author meta tag, so only JSON-LD has them. See Combining parsers.

What CI checks ​

The selected node, both resolved references, the dates with their offset, sections and tags with their @id, and the merged result of both parser orders.

Released under the MIT License.