You’ve left something out of the description– namely, it isn’t clear how/where you load that code. I am guessing you are pushing things through a template file somehow, or through a backend plugin file maybe, as you have what looks like template code showing up.
You should instead, in my opinion, be using the AJAX API:
function my_grab_ajax_wpse_108874() {
if (empty($_GET['need_options'])) die;
$need_options = $_GET['need_options']; // ??????
$need_options = get_post($need_options);
$json_name = $need_options->post_name;
$json_file = json_encode($need_options); // Encode data into json data
$json_file = stripslashes($json_file);
header("Content-Type: text/json; charset=" . get_option( 'blog_charset'));
header("Content-Disposition: attachment; filename=$json_name.json");
echo $json_file;
exit();
}
add_action('wp_ajax_my_grab_ajax', 'my_grab_ajax_wpse_108874');
add_action('wp_ajax_nopriv_my_grab_ajax', 'my_grab_ajax_wpse_108874');
Then request the data with http://example.com/wp-admin/admin-ajax.php?action=mygrab_ajax&need_options={some post ID}