Rich-text metafield creating invalid JSON; double quotes not escaped

Rich-text metafield creating invalid JSON; double quotes not escaped

techheritagebks
Shopify Partner
4 0 3

TL;DR: Shopify rich text metafield does not escape double quotes, making JSON invalid

 

I created a metafield for Products that is rich text (Metafield: custom.shortdescription [rich_text_field]). I added content to that field through the admin interface. I was doing a bulk update of the products' metafields, so I exported all of the metafields. The fields I had changed updated fine, but one product failed the update with error message: Error saving Metafield [custom.shortdescription] - [Value is invalid JSON: unexpected token at '{"type":"text","value":"Lillian.....

 

I have since found what caused the problem: the metafield content contained double quotes ("). To get the JSON to format correctly, those double quotes have to be escaped [ " actually has to be \\\"  where the first two characters are an escaped backslash and the second two are the escaped double quote] . Somehow, this is not happening if you add double quotes in the RTF entry box. Short of never using double quotes in these short descriptions [NOT an option], do I have to always check for these and manually add the escape characters every time via a bulk upload?

 

BTW, I am using Matrixify for the import/export. If I export via Shopify native import/export, RTF fields are not exported, so I can't tell how Shopify itself stores the RTF and if the escaped character issue arises from Shopify or Matrixify.

 

EDIT: Are there any other characters that need to be escaped that I have to watch out for?

Replies 2 (2)

Renars
Shopify Partner
305 32 363

Hi @techheritagebks 

 

Thank you for using the Matrixify app for this task.

Matrixify app would import the JSON code as you have it in the import file as it is essentially just a text field in Excel.

 

The JSON code in your file does need to be valid for Shopify API to accept it and thus needs to be properly escaped.

Mostly as I have tested, if your JSON code validates and works in online JSON code validators then it would also work when imported in Shopify.

 

The entry box in admin does not have this issue as in the admin it is not being displayed as JSON code and you do not enter JSON code in there.

But either if its Rich Text or any other metafield that is stored as JSON code, when you handle the actual JSON code, it does need to be valid JSON and thus needs to have any such characters escaped.

So it is really neither Matrixify nor Shopify's fault, it is just how JSON code must be managed and handled.

 

On a quick Google search found this page going over what special characters need ot be escaped.

Matrixify | Bulk Import Export Update | https://apps.shopify.com/excel-export-import | https://matrixify.app
techheritagebks
Shopify Partner
4 0 3

If I enter text containing special characters in the rich text metafield, I would expect it to be escaped correctly when converted to JSON by the back end of Shopify.

 

For example, if I was to type in the box

JSON requires double quotes (") to be escaped

I would expect the JSON text I export to be:

{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"JSON requires double quotes (\\\") to be escaped"}]}]}

NOT

{"type":"root","children":[{"type":"paragraph","children":[{"type":"text","value":"JSON requires double quotes (") to be escaped"}]}]}

 

Shopify is doing the latter, which means if I export that product, then import the doc without making any edits, I receive the invalid JSON error.

 

It's not so bad if it's only one or two, but we have over 13K of products. Many of them contain double quotes in the content that will be in this field.