jQuery uses a non-breaking space nbsp
or charcode \xA0
instead of a normal space, so you will need to replace it.
let text = $('#testBox').val();
let jsonObject;
try {
jsonObject = JSON.parse(text);
} catch (ex) {
console.log("error: ", ex.message)
}
text = text.replace(/\xA0/g, " "); // or \s but will also replace newline
jsonObject = JSON.parse(text);
console.log("what the name: ", jsonObject.name)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="testBox">
{
"name": "Foobar",
"favorite_pets": ["capybara", "lizard"],
"favorite_fruits": ["avocado"],
"address": {
"city": "FairyVille",
"street": "42 Main St."
}
}
</textarea>