When setting up Structured Data using JSON-LD for the first time you could run into a problem with the articleBody property which mostly consists of a lot of html tags, quotation marks, etc. These can cause all sorts of parsing errors when you run your page through the Google’s Structured Data testing tool, but thankfully you can solve the issue with just one php function.
There was an error parsing your JSON-LD
As a developer I’m sure you can relate to the pain I felt when I finally got the whole json-ld code implemented on my WordPress blog just to see this error message show up in Structured Data testing tool.
It’s true that Google hasn’t really put a lot of effort into explaining what’s wrong with your code, but at least they’re highlighting the area where the error occurred which should give us some clues for coming up with a solution on our own. On the screenshot you can see that a parsing error happened when it reached “G so I immediately realised there was something wrong with the double quotes.
Since I’m relatively new to json-ld I sometimes miss a comma or a colon, but with this particular problem I had to somehow escape the double quotes only because when you escape all of the quotes you’ll run into even more problems that I am not going to cover here today.
So, how do you escape only the double quotes and leave single quotes untouched?
Turns out php has a great little function for that:
addcslashes($str, ' " ')
What this does is it takes your string ($str) and escapes only the characters you explicitly tell it to escape. The example above will escape double quotes only because that’s what we tell it to do.
Real world example
Using the above function I implemented it along with the wp_strip_all_tags() function for WordPress because let’s only send plain text to search engines inside the articleBody property 😉
"articleBody": "<?php echo addcslashes (wp_strip_all_tags( get_the_content() ),'"');?>",
As you can see I’m echoing the WordPress post content by stripping all of its html tags and making sure double quotes are escaped properly as well.
You should do the same to your description property if you use it because who knows, maybe you have used double quotes in there too?
After doing this I no longer get the json-ld parsing error and you should just ignore the one warning I have in my Article results because that’s about an image and its width not being big enough. Completely unrelated!