'Make JSON.parse work for array of objects with quotation mark

I am trying to store an array of object as string and restore it later. However, some elements in the objects are string and they may have ' or ". This causes an issue when I try to convert back and forth this array of objects to string with JSON.parse. Is there an standard way to resolve this issue?

 vars = [{'date': '04-29', 'x': 'this "x" ', 'y': '<p><img src="data:image/jpeg;base64,/9j/4AADEQA/ANmAA9KdX/txX0/2Q=="></p>'}]


Solution 1:[1]

try this

vars=JSON.parse(JSON.stringify(vars));

result

[{"date":"04-29","x":"this \"x\" ","y":"<p><img src=\"data:image/jpeg;base64,/9j/4AADEQA/ANmAA9KdX/txX0/2Q==\"></p>"}]

or this ( I don't know what you want to reach)

vars = JSON.parse(JSON.stringify(vars).replaceAll("\\\"","'"));

result

[{"date":"04-29","x":"this 'x' ","y":"<p><img src='data:image/jpeg;base64,/9j/4AADEQA/ANmAA9KdX/txX0/2Q=='></p>"}]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Serge