1. The existing code implementation is as follows:
PHP
"url": "{{ json_encode($item['url_object']) }}",
2. The final generated JSON data structure is as follows:
PHP
"url": "{"ID":18,"title":"summer","object":"product_cat","object_id":18,"children":[],"url":"/collections/summer","key":3991}",
3. Blade’s {{ }} statements are automatically passed through PHP’s htmlspecialchars function to prevent XSS attacks. Therefore, you need to use {!! !!} to prevent ” (double quotes) from being converted into HTML entities.
PHP
"url": "{!! htmlspecialchars(json_encode($item['url_object']), ENT_NOQUOTES | ENT_HTML401) !!}",
4. However, the generated JSON structure is incorrect. The reason is that the value of url is not escaped. See Figure 1.

JSON
{
"image": "",
"mobile_image": "",
"url": "{"ID":18,"title":"summer","object":"product_cat","object_id":18,"children":[],"url":"\/collections\/summer","key":3991}",
"title": "summer"
}
5. Use addcslashes — to escape characters in a string using C-language style backslashes.
PHP
"url": "{!! htmlspecialchars(addcslashes(json_encode($item['url_object']), "\f\n\r\t\\\""), ENT_NOQUOTES | ENT_HTML401) !!}",
6. The generated JSON structure is correct. See Figure 2.

JSON
{
"url": "{\"ID\":18,\"title\":\"summer\",\"object\":\"product_cat\",\"object_id\":18,\"children\":[],\"url\":\"\\/collections\\/summer\",\"key\":3991}",
"image": "",
"title": "summer",
"mobile_image": ""
}
