Brad Madigan
Brad Madigan

Reputation: 299

Shopify - API Error when adding a new product (Lexical JSON error)

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));

Upvotes: 2

Views: 932

Answers (1)

Sandeep Shetty
Sandeep Shetty

Reputation: 1250

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.

Upvotes: 2

Related Questions