Invalid JSON: '0x0A' is invalid within a JSON string. The string should be correctly escaped. Path: $.body | LineNumber: 3 | BytePositionInLine: 79

Hi Sebastian,

I think I’m going to need your help with this one. I’ve checked both the forums and docs.squidex.io but couldn’t find much info.

The problem is that I’m populating my index at Algolia but the JSON sent over from Squidex seems broken. Here are the reproduce steps:

  1. Set up your schemas, rules, etc so that you have a working Algolia integration.

  2. Make sure you have a body field in your schema (see screenshot)

  3. Add the Liquid script:

{
    "title": "{{event.data.title.en-US}}",
    "description": "{{event.data.description.en-US}}",
    "body": "{{event.data.body.en-US | html2text}}",
    "author": "{{event.data.author.iv}}",
    "authorRole": "{{event.data.authorRole.en-US}}",
    "publishedDateTimestamp": "{{event.data.publishedDateTimestamp.iv}}"
}
  1. Update your body field content with:
<p class="lead">
  Until now, trying to style an article, document, or blog post with
  Tailwind has been a tedious task that required a keen eye for
</p>

and then check your Algolia index; you will see the following error message there:

"Invalid JSON: '0x0A' is invalid within a JSON string. The string should be correctly escaped. Path: $.body | LineNumber: 3 | BytePositionInLine: 79."


According to ChatGTP:

The character 0x0A is a newline character in hexadecimal notation. In JSON, newline characters within strings must be properly escaped.


The newline character that the Algolia error message is referring to is the character between “with” and “Tailwind” in the HTML markup above.

In the meantime I could create a new plain text (no markup) body field and index that but then I would need to maintain two fields for each blog post which is not ideal. Is there a better work-around in the mean time?

Thanks!

You have to encode the fields manually with | escape

So this:

"body": "{{event.data.body.en-US | html2text}}",

should be like this?

"body": "{{event.data.body.en-US | escape }}",

"body": "{{event.data.body.en-US | escape }}",

seems to do the trick. Thanks, Sebastian! :slight_smile:

1 Like

I have added it to the docs: https://docs.squidex.io/id-02-documentation/developer-guides/rule-formatting/liquid#how-to-handle-json

1 Like