Malformed JSON-LD found

Description

Rule ID: malformed-json-ld

The rule checks structured data LD+JSON for issues that prevent parsing schema. Especially as it relates to the error:

[Facebook Pixel] – Unable to parse JSON-LD tag. Malformed JSON found:

Context

It is important to have well-defined structured data LD+JSON. But when the structure is malformed then it impacts the processing by search engines, as they will have difficulties understanding the structure. The rule checks for whitespaces and ensures it gets parsed without issues.

How to fix it

  1. Make sure your structured data LD+JSON is being represented as a single string, without whitespaces, especially new lines.

    Example of formatted JSON
    {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      "mainEntity": [{
        "@type": "Question",
        "name": "How are you doing?",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "I am doing fine."
        }
      }]
    }
    Example of JSON as a single line
    {"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How are you doing?","acceptedAnswer":{"@type":"Answer","text":"I am doing fine."}}]}
  2. HTML in your JSON must be escaped. Otherwise it won’t be parsed.

    Example of invalid JSON (contains unescaped HTML)
    {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      "mainEntity": [{
        "@type": "Question",
        "name": "How are you doing?",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "I am doing <strong>fine</strong>"
        }
      }]
    }
    Example of valid JSON (contains escaped HTML)
    {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      "mainEntity": [{
        "@type": "Question",
        "name": "How are you doing?",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "I am doing &lt;strong&gt;fine&lt;/strong&gt;"
        }
      }]
    }
  3. If you’re using WordPress plugin Markup (JSON-LD) structured in schema.org then enable (check the checkbox) option Compress output data under section Schema.org Config.
  4. For PHP language use: json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES).