Discussing website operation experts: How to elegantly handle JSON-LD null values in AnQi CMS and create high-quality structured data

In today's digital marketing environment, structured data (JSON-LD) has become a key to improving website search engine visibility and obtaining rich media summaries (Rich Snippets).It can help search engines understand web content more accurately, thus providing users with more precise search results.AnQiCMS (AnQiCMS) is an efficient content management system tailored for enterprises and content operation teams, and its built-in SEO optimization features naturally also include support for JSON-LD.

However, in the actual content operation, we often encounter such challenges: some fields in the content model may be empty, or may not exist in certain scenarios.If the empty values or missing fields are directly output to JSON-LD, it may not only lead to the failure of structured data validation, but also trigger warnings in the search engine site master tools, and even affect the display effect of rich media summaries.How can we skillfully handle the situation where fields are empty or do not exist in JSON-LD as experienced users of AnQiCMS, ensuring that the output data is always valid and high-quality?

This article will reveal an effective solution based on the powerful template engine and flexible tag features of AnQiCMS.

Secure CMS and JSON-LD: Flexible Integration Mechanism

AnQiCMS understands the importance of structured data and provides high flexibility to manage your JSON-LD output. Through{% jsonLd %}Label, you can easily insert a custom JSON-LD code block in the template. AnQiCMS will process the JSON-LD code block you have entered.{% jsonLd %}The JSON content defined within the tag is intelligently merged with the system-generated JSON-LD data.This means that you can optionally override the default fields or add additional custom fields to meet complex structured data requirements.

But it is this flexibility that requires us to be extra meticulous when handling data.When dynamically extracting data from the AnQiCMS content model to fill JSON-LD, we must consider the possibility that fields may be blank or have no data.

Core Strategy One: Use conditional judgment to output fields as needed.

The most direct and effective way to avoid outputting invalid data is to use the AnQiCMS template engine's{% if %}The tag makes conditional judgments. Before writing any data field into JSON-LD, we should first check if the field exists or contains valid content.If the field is empty, we do not output it to JSON-LD to maintain the cleanliness and validity of the data structure.

Let us take an example of a common Article type JSON-LD.Assuming we need to output the description (description) and featured image (image), but these fields may be missing in some articles.

{% jsonLd %}
<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": "{% archiveDetail with name='Title' %}",
{%- archiveDetail archiveDesc with name='Description' -%} {# 先将描述数据赋值给 archiveDesc 变量 #}
{%- if archiveDesc -%} {# 判断 archiveDesc 是否存在或有值 #}
    "description": "{{ archiveDesc }}",
{%- endif -%}
{%- archiveDetail archiveLogo with name='Logo' -%} {# 先将 Logo 数据赋值给 archiveLogo 变量 #}
{%- if archiveLogo -%} {# 判断 archiveLogo 是否存在或有值 #}
    "image": "{{ archiveLogo }}",
{%- endif -%}
    "datePublished": "{% archiveDetail with name='CreatedTime' format='2006-01-02T15:04:05-07:00' %}",
    "author": {
        "@type": "Organization",
        "name": "{% system with name='SiteName' %}"
    }
}
</script>
{% endjsonLd %}

In the above example, we first use{% archiveDetail with name='Description' %}and{% archiveDetail with name='Logo' %}The label assigns the description of the article and the address of the Logo image toarchiveDescandarchiveLogotwo temporary variables. Then, we use{% if archiveDesc -%}and{% if archiveLogo -%}Perform conditional judgment. Only when these variables contain actual content, the corresponding JSON-LD field (such as"description"and"image"Will be output. If the variable is empty, the entire field along with its comma separator will not be rendered in the final JSON-LD, thus avoiding output""ornullSuch a null value ensures the validity of JSON-LD.

Core strategy two: provide default values or alternative content

Sometimes, simply skipping empty fields may not be a **choice. For some required but possibly empty fields (from an SEO perspective), providing a reasonable default or alternative content is better.AnQiCMS template filterdefaultanddefault_if_noneIt can be used here.

  • defaultFilter: When the variable is an empty string, zero, or an empty array orfalse, provide a default value.
  • default_if_noneFilter: Specifically used to determine whether the variable isnil(null pointer), if it isnilthen provide a default value.

Continue with the above article JSON-LD example, assuming that even if the description is empty, at least the article title should be used as a backup description, or a generic default image URL should be provided:

”`twig {% Ld %}