javascript – How to extract with jQuery json from data attribute with &quot inside?

I store JSON in a data attribute, which contains &quot:

<div id="tt" data-json='{"t":"title: &quot;BB&quot;"}'></div>

I get the data-json value with jQuery:

aa1 = $('#tt').data('json');

However, I get a string aa1, not an object as I would expect. If I delete &quot from the JSON, then I get an object in aa1.

If I do so:

aa1_str = $('#tt').attr('data-json');
aa1 = JSON.parse(aa1_str);

I am getting an error in JSON.parse:

Uncaught SyntaxError: Unexpected token B in JSON at position 14

$('#tt').attr('data-json') returns an already parsed string, where &quot is replaced with ".

How can I correctly extract data from a JSON string which contains &quot?

Read more here: Source link