Website operation expert discusses: How to elegantly handle JSON-LD null values in AnQi CMS to 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.安企CMS(AnQiCMS)as a highly efficient content management system tailor-made for enterprises and content operation teams, its built-in SEO optimization features naturally also cover 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 at all in certain scenarios.If the null values or missing fields are directly output to JSON-LD, not only will it cause the structured data validation to fail, but it may also trigger warnings in the site owner tools of search engines, even affecting the display effect of rich media summaries.How should we skillfully handle the situation where the field is empty or does not exist in JSON-LD as an experienced AnQiCMS user, ensuring that the output data is always valid and of high quality?

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

Automated CMS with JSON-LD: Flexible Integration Mechanism

AnQiCMS knows the importance of structured data and provides high flexibility to manage your JSON-LD output. Through{% jsonLd %}标签,您可以轻松地在模板中插入自定义的JSON-LD代码块。AnQiCMS会将您在{% jsonLd %}The JSON content defined within the tag is intelligently merged with the default generated JSON-LD data.This means, 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 particularly meticulous when handling data.When dynamically extracting data to fill JSON-LD from the AnQiCMS content model, we must consider the possibility that fields may be blank or have no data.

核心策略一:利用条件判断,按需输出字段

The most direct and effective way to avoid outputting invalid data is to use the AnQiCMS template engine,{% if %}Label for conditional judgment.Before writing any data field to 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 the JSON-LD, thus maintaining the neatness and validity of the data structure.

Let us take a common article (Article) type JSON-LD as an example.The description (description) and featured image (image) of the article need to be output, 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 and Logo image address of the article to two temporary variables.archiveDescandarchiveLogoNext, we use{% if archiveDesc -%}and{% if archiveLogo -%}Perform conditional judgment. Only when these variables contain actual content, the corresponding JSON-LD fields (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, merely skipping empty fields may not be**the choice.For certain mandatory fields that may be empty (from an SEO perspective), it is better to provide a reasonable default value or alternative content.defaultanddefault_if_noneThis is where it comes into play.

  • defaultFilter: When the variable is an empty string, zero, or an empty array,falsea default value should be provided.
  • default_if_noneFilter: Specifically used to determine whether the variable isnil(null pointer), if it isnil, then a default value should be provided.

Continue with the JSON-LD example above, assuming we want to use the article title as a fallback description even if the description is empty, or provide a generic default image URL:

`twig {% Ld %}