Can't parse JSON from data attribute

Mr. Jo :

I'm currently trying to parse a JSON from a data attribute but I'm getting an error:

Uncaught SyntaxError: Unexpected token � in JSON at position 0

This is my code:

jQuery( document ).ready( function ( $ ) {
	let variations = $( "span" ).data( "variations" );

	$( JSON.parse( variations ) ).each( function ( index, variation ) {
		console.log( variation );
	} );
} );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span data-variations='["\ud83d\udc36","\ud83d\udc31"]'></span>

I don't get it. I'm printing the JSON in PHP with this function but I'm stuck in my head here:

<span data-variations='<?= esc_html( json_encode( $variations ) ) ?>'></span>
Seblor :

You are having this issue because variations already has the parsed value, as said in the doc :

When a string starts with '{' or '[', then jQuery.parseJSON is used to parse it

jQuery( document ).ready( function ( $ ) {
	let variations = $( "span" ).data( "variations" );

	$( variations ).each( function ( index, variation ) {
		console.log( variation );
	} );
} );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span data-variations='["\ud83d\udc36","\ud83d\udc31"]'></span>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301935&siteId=1