Remove quotes from a json object value

I am using Woocommerce API in node js to import product data from mssql server db to my woocommerce database.

The recordset, I am getting through ms sql query, contains a images field.

Read More

In Woocommerce API, to import images I need this type of json object,

var data = {
  product: {
    title: 'Premium Quality',
    type: 'simple',
    regular_price: '21.99',
    description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
    short_description: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    categories: [
      9,
      14
    ],
    **images: [
      {
        src: 'http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg',
        position: 0
      },**
      {
        src: 'http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg',
        position: 1
      }
    ]
  }
};

Rest is fine, but I have issue with images, As I am getting images json in quotes images: '[{...}]' as the data type is varchar in db table.

Because of this quotes, Images are not inserting in db through Woocommerce rest API.

I want to remove the quote from images specifically. Or any other suggestion.

I am new to node js. Can any one please help.

Related posts

1 comment

  1. As far as I understood you problem is you are getting extra "" at the start and end of a string which specifies the image location.
    You just have to chop of the first & last character of your string variable in js.

    Solution:

    var x = String(/'[{...}]'/);
    str = x.substring(1, x.length - 1);
    var image = str.substring(1, str.length - 1);
    

    Demo :

    function myFunction() {
      var x = String(/'[{...}]'/);
      str = x.substring(1, x.length - 1);
      var res = str.substring(1, str.length - 1);
      document.getElementById("demo").innerHTML = res;
    }
    <button onclick="myFunction()">Try it</button>
    
    <p id="demo"></p>

    OR

    Demo:

    Remove “” from start and end of string :

    var someStr = String('[{...}]');
    alert(someStr)
    alert(someStr.replace(/^"(.*)"$/, '$1'));
    

    var someStr = String('"[{...}]"');
    alert(someStr)
    alert(someStr.replace(/^"(.*)"$/, '$1'));

    OR

    var test = ""House"";
    console.log(test);
    console.log(test.replace(/"/g, "")); 
    

    Refer : remove-double-quotes-from-json-return-data and Can you create a JavaScript string without using ‘ or ” quotes?

Comments are closed.