I have a huge list of things with each of those things broken down into four sub-things. I am easily able to gather them in nested arrays in PHP, but when I use json_encode to pass them as a JSON object to my javascript client side, the JSON.parse() function always fails because my data is filled with all kinds of entities like ' / \ " & % {} []
It's a shame there is no constant that you can add to json_encode to ensure it's javascript ready.
What characters must I escape or otherwise deal with to ensure I get a javascript parsable object. I was unable to find one. I could easily write a function to handle this if I knew which characters to deal with.
There should be no problem with your approach. Maybe there is a problem when rendering the data? Did you try the following to output the json?
echo 'var myjs = ' . htmlspecialchars(json_encode($obj)) . ';';
If you render the json in a different url/endpoint you do not have to encode anything yourself. However you should make sure your http headers are set correctly
header('Content-Type: application/json');
echo json_encode($obj);
die(0); # thx for requesting my endpoint