2

When a merchant adds a new product, as long as they type normal text into the body_html field, it works great. However, when they try to add some HTML from either copy paste or adding image into the WYSIWYG editor (that has "") we get the famous:

lexical error: invalid char in json text.

Now, they may be pasting from an unknown source, is there a way any has figured out how I could possible clean up the body_html before sending it on to ShopifyAPI?

By the way, I'm using PHP and the wcurl.php https://github.com/sandeepshetty/wcurl

UPDATE:

lexical error: invalid char in json text.

              "{"product":{"title":"Sample Event
    (right here) ------^

CODE SAMPLE:

$shopify_data = array
(
   "product"=>array
   (
     "title"=>$rs->product_title,
     "body_html"=>$rs->product_details,
     "vendor"=>"My Companay",
     "product_type"=>"Laptop"
   )
);

foreach ($variant as $key => $value) {
  $shopify_data["product"]["variants"][$key] = array(
    "option1"=> $value->variant_name,
    "price"=> $value->price,
    "requires_shipping"=>'true',
    "inventory_management"=>"shopify",
    "inventory_quantity"=> $value->quantity
  );
}

// $shopify_data = json_encode($shopify_data);  // This does not work either.
$shopify_data = stripslashes(json_encode($shopify_data));

1 Answer 1

2
+50

If I understand this right the solution is:

stripslashes(json_encode($params))

I do this in my Shopify client: https://github.com/sandeepshetty/shopify_api/blob/1f538276e690bd7b95f9cbb4007576ecb2d3f6de/client.php#L52

Note: PHP 5.4.0 has an option for this in json_encode.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Sandeep. I tried that as well. I put the code sample at the bottom of my question.
Hey Sandeep. I just realized, I'm using your Shopify client code which does it for you
So, if I just pass the Array() without the StripSlashes or Json_Encode, it works. Which is expected. BUT, if I put HTML into the Body of the Product body_html, it breaks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.