Answer the question
In order to leave comments, you need to log in
How can I replace single quotes with double quotes in invalid JSON?
But you also need to take into account that in JSON there are pieces of html code with doubles that are nested in singles (at the moment).
Thanks everyone for the replies!
Answer the question
In order to leave comments, you need to log in
Maybe not the best solution, but in haste:
'use strict';
const invalidJSON = `{"root": {'html': '<div class="header1"></div>', 'fragment': '<div class="footer"></div>'}}`;
let validJSON = invalidJSON.replace(/'/g, '"');
validJSON = validJSON.replace(/="+[A-z0-9]*"/g, (str) => {
return str.replace(/"/g, '\\"');
});
console.log(validJSON);
// => {"root": {"html": "<div class=\"header1\"></div>", "fragment": "<div class=\"footer\"></div>"}}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question