How can you generate and validate structured data with JavaScript?

ProcrustesPR

You can use JavaScript to generate JSON-LD and insert it into a web page, then check the implementation with the Rich Results Test. Running the code is not enough: the rendered HTML must actually contain the structured data so Google Search can process it. Google’s documentation describes using custom JavaScript or Google Tag Manager (GTM), or including the data in server-rendered output. For validation, it recommends entering a URL rather than simply pasting code.

ProcrustesPRProcrustesPRHow Long Does It Take to Evaluate SEO Results?

Choose the data type first, then decide how to generate it

First, identify the structured data type appropriate for the page, then prepare the properties according to the documentation for that type. Custom JavaScript can generate all the data or supplement structured data already output by the server. If you use server-side rendering, follow your framework’s documentation to include JSON-LD in the output.

In the recipe example in Google’s documentation, JavaScript first retrieves the data as text using fetch. It then creates a script element and sets its type to application/ld+json. Next, it writes the retrieved content to the element’s textContent and appends the element to document.head. This example illustrates the insertion process; you still need to check the documentation for the data type to determine whether the recipe data is complete.

When using GTM, keep the markup aligned with the page content

To use GTM, install a container, add a Custom HTML tag with the structured data block, then publish the container and test the results. Google recommends using variables to read the necessary information from the page. This avoids maintaining a separate copy that could become inconsistent with the page content.

  • If the recipe name uses the page title, you can create a recipe_name variable that returns document.title.
  • The image and author can also be supplied through recipe_image and recipe_author variables; define these variables before referencing them.
  • The name, image, and author are examples only. Providing these properties alone does not necessarily meet all requirements for that type.

Test the URL and check the rendered output

  1. Open the Rich Results Test, enter the URL of the deployed page, and select Test URL. The documentation recommends using a URL because JavaScript is subject to limitations such as CORS restrictions when you enter code directly.
  2. If the implementation is correct and the tool supports the data type, it will display “Page is eligible for rich results.” This means the page is eligible, not that rich results are guaranteed to appear.
  3. If errors or warnings appear, first check for syntax errors and missing properties. The documentation identifies these as the most likely causes, but not the only ones.

If the tool does not support the type, inspect the rendered HTML to confirm whether the data is present. Do not conclude that insertion failed solely because the tool does not list any results.

Additional conditions apply to product markup and indexing settings

Google’s documentation cautions that dynamically generated Product markup may make Shopping crawls less frequent and less reliable, affecting rapidly changing information such as price and availability. Merchants optimizing for all shopping results must also ensure their servers can handle increased traffic from Google.

Additionally, if the original HTML contains noindex, Google may skip rendering and JavaScript execution. If you want the page to be indexed, do not rely on JavaScript to remove this tag later.

ProcrustesPRProcrustesPRWhere should I start if I want to do SEO myself?

JavaScript structured data: perspectives from across the web

Can I use a generator if I don’t want to write JSON-LD from scratch?

Yes. TechnicalSEO’s Schema Markup Generator can help you create JSON-LD. When you need structured data, choose an appropriate type based on the page’s actual content. After generating it, you still need to check that the markup matches the page content, then validate and deploy it according to its intended use. It is a markup generator, not a substitute for a site architecture audit.

Will adding structured data improve Google rankings?

That cannot be guaranteed. Structured data can provide search engines with context about content and may help it appear in rich formats, potentially improving search visibility. However, Semrush notes that it is not a confirmed Google ranking factor. Successfully generating markup with JavaScript does not mean rankings will necessarily improve.

If AI search does not cite a website, is it because special Schema markup is missing?

You cannot draw that conclusion directly. Google explains that AI search does not require special Schema markup. Structured data can still be part of SEO work and help establish eligibility for certain rich results. However, a markup generator cannot diagnose why AI has not cited a website, and low visibility should not be attributed directly to missing Schema markup.

ProcrustesPRShould I rewrite or update an old article that does not rank?

References

  1. 瞭解 JavaScript 搜尋引擎最佳化 (SEO) 基礎知識 | Google 搜尋中心  |  Documentation  |  Google for Developersdevelopers.google.com — Source used in the article above.
  2. 利用 JavaScript 產生結構化資料 | Google 搜尋中心  |  Documentation  |  Google for Developersdevelopers.google.com — Source used in the article above.
  3. JavaScript Rendering: What It Is and How to Handle Itwww.semrush.com · 2023-04-03T15:07:00Z — Discusses: Will adding structured data improve Google rankings?
  4. GEO 工具有哪些?2026 十大 GEO 優化工具一次整理www.da-vinci.com.tw · 2026-10-01T16:34:26+08:00 — Discusses: Can I use a generator if I don’t want to write JSON-LD from scratch?; If AI search does not cite a website, is it because special Schema markup is missing?

Related articles

How Long Does It Take to Evaluate SEO Results?Where should I start if I want to do SEO myself?Should I rewrite or update an old article that does not rank?What should I check when organic traffic suddenly drops?Why can I not find my published article on Google?Will Reposting Social Content on My Website Cause a Duplicate-Content Penalty?Eight SEO essentials for beginners: Getting your pages indexedWhat Is SEO? Understanding Optimization Priorities Through How Search Engines Work
ProcrustesPR
ProcrustesPR