use json_encode on variables nested in JS

used json_encode to properly escape variables