This question, this one, and similar ones did not help.
The JSON below is valid.
However, invoking JSON.parse throws an error.
How can you parse valid JSON from a textarea element?
JSfiddle: https://jsfiddle.net/6gpdL81e/
let text = document.getElementById("testBox").value;
let jsonObject = JSON.parse(text);
<textarea id="testBox">
{
"name": "Foobar",
"favorite_pets": ["capybara", "lizard"],
"favorite_fruits": ["avocado"],
"address": {
"city": "FairyVille",
"street": "42 Main St."
}
}
</textarea>
$('#testBox').val().replace(/[^!-~]/g, '');to effectively remove all characters that fall outside of the char range 33-126, but really I wouldn't clean it and would just return an error to the user if the parse fails. Because there could legitimately be a valid parse error that you are going to have to handle.