Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON-LD and quotes in the content

Tags:

php

json-ld

I have an issue with validating JSON-LD code by Google Structured Data Testing Tool. My article text is taken from MySQL database and put into JSON-LD structure by php script along the following lines:

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "Article",
    ...
    "articleBody": "<?php echo article content here ?>",
    ...
}

The problem is that if the article text contains quotation marks (") then they conflict with JSON-LD block syntax and cause validation error. Basically the very first instance of (") in the article content indicates the end of articleBody and the next (") causes syntax error.

The only idea I have is to pre-process article content with php and remove (") symbols. This works fine, but looks artificial:

    "articleBody": "<?php echo str_replace( '"', '', article content here) ?>",

Are there any standard workarounds available?

like image 545
sergekol Avatar asked Oct 17 '25 12:10

sergekol


1 Answers

Since you are trying to output JSON, I would suggest use json_encode instead, which should be more straightforward, and less likely to be broken.

<script type="application/ld+json"><?=json_encode([
    "@context" => "http://schema.org",
    "@type" => "Article",
    "articleBody" => $articleBody,
    ...
])?></script>

Result will be:

<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"Article","articleBody":"Your article body"}</script>
like image 66
CQD Avatar answered Oct 19 '25 02:10

CQD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!