I use the following template code to write the JSON file:
<?php
/**
Template Name: Programació JSON
Gets all appointments and associated podcast data from DB
*/
header('Content-Type: application/json; charset=utf-8');
$fp = fopen('php://output', 'w');
$params = array();
$query_programacio = $wpdb->prepare("SELECT id,date,start
FROM wp_ea_appointments
WHERE date >= CURDATE()
ORDER BY date ASC,start",
$params
);
$programacio = $wpdb->get_results($query_programacio);
//echo json_encode(array("data" => $programacio));
$i = 0;
foreach ($programacio as $prog) {
$query_podcast = $wpdb->prepare("SELECT value
FROM wp_ea_fields
WHERE app_id=$prog->id AND field_id=5",
$params
);
$podcast->id = (int)$wpdb->get_results($query_podcast)[0]->value;
$programacio[$i]->title = get_post($podcast->id)->post_title;
$programacio[$i]->author_id = get_post($podcast->id)->post_author;
$programacio[$i]->author_name = get_the_author_meta('display_name', $programacio[$i]->author_id);
$i++;
}
echo json_encode(array("podcasts" => $programacio));
fclose($fp);
?>