Search code examples
javascriptparsingreplaceformatstringify

JSON.parse incorrect string format


i have this string:

"{\\'Ovcount\\':\\'0\\',\\'S1\\':\\'LU\\',\\'S2\\':\\'NewClientOrMove\\',\\'memoToDisplay\\':\\'LU -- New Client or Move\\\"}"; 

and i want it to become like this:

'{"Ovcount":"0","S1":"LU","S2":"NewClientOrMove","memoToDisplay":"LU -- New Client or Move"}'

I tried with stringify and replace and i ended up with

"{'Ovcount':'0','S1':'LU','S2':'NewClientOrMove','memoToDisplay':'LU -- New Client or Move"}"

And from here i wanted to replace the single quotation marks ' with double quotation marks " but when i did, in beginning and ending of the string appeared an extra "\

"\ "{"Ovcount":"0","S1":"LU","S2":"NewClientOrMove","memoToDisplay":"LU -- New Client or Move\"}\ ""

Any tips on how to get the correct format?


Solution

  • var result = "{\'Ovcount\':\'0\',\'S1\':\'LU\',\'S2\':\'NewClientOrMove\',\'memoToDisplay\':\'LU -- New Client or Move\"}"
      .replaceAll("'", '"')
      .replaceAll('\\', '');
    console.log(result);

    this got me what you wanted, it replaces all the single quotes with double quotes, then it removes any back slashes

    the result is

    '{"Ovcount":"0","S1":"LU","S2":"NewClientOrMove","memoToDisplay":"LU -- New Client or Move"}'