I'd hate to open a new question even though many questions have been opened on this same topic, but I'm literally at my ends as to why this isn't working.
I am attempting to create a JSON object with the following code:
var p = JSON.stringify(decodeJSON('{{post.as_json}}'))
var post = JSON.parse(p);
console.log(post); // Debug log to test if code is valid
And the decodeJSON
function:
function decodeJSON(json) {
var txt = document.createElement("textarea");
txt.innerHTML = json;
return txt.value.replace(/u'/g, "'");
}
console.log(post)
returns the following JSON string:
{'content': 'kj fasf', 'uid': '4eL1BQ__', 'created': '07/09/2017', 'replies': [], 'tags': ['python'], 'by': {'username': 'Dorian', 'img_url': '/static/imgs/user_Dorian/beaut.jpg'}, 'likes': 0}
After scanning through the string I am pretty sure that the JSON is valid and there are no syntax errors. However, when running JSON.parse(p)
Instead of receiving an object, I get a string back. What could be the cause?