rob.m
rob.m

Reputation: 10591

How to modify json to validate it?

I am downloading some json and merging them, but while doing that it creates a string like below,

    }
][
    {

I'd like to replace that with a comma in order to have a valid json

I tried something like this but it's not working:

$datas = array();
$json = str_replace("][",",", $datas);
$json = json_encode($json, JSON_PRETTY_PRINT);
print_r($json);

If I can manage to do that I'd use the same to replace columns name (I'm actually building this json from a csv) if possible.

UPDATE

To clarify I have multiple csv(s) from which I am then building a single json

FULL CODE

header('Content-Type: application/json');

$arr = array("04-12-2020",
    "04-13-2020",
    "04-14-2020",
    "04-15-2020",
    "04-16-2020",
    "04-17-2020",
    "04-18-2020",
    "04-19-2020",
    "04-20-2020",
    "04-21-2020",
    "04-22-2020",
    "04-23-2020",
    "04-24-2020",
    "04-25-2020",
    "04-26-2020",
    "04-27-2020",
    "04-28-2020",
    "04-29-2020",
    "04-30-2020",
    "05-01-2020",
    "05-02-2020",
    "05-03-2020",
    "05-04-2020",
    "05-05-2020",
    "05-06-2020",
    "05-07-2020",
    "05-08-2020",
    "05-09-2020",
);

foreach($arr as $date) {

    $url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports_us/".$date.".csv";

    if (($handle = fopen($url, "r")) !== FALSE) {

        $csvs = [];
        while(! feof($handle)) {
            $csvs[] = fgetcsv($handle);
        }
        $datas = [];
        $column_names = [];
        foreach ($csvs[0] as $single_csv) {
            $column_names[] = $single_csv;
        }
        foreach ($csvs as $key => $csv) {
            if ($key === 0) {
                continue;
            }
            foreach ($column_names as $column_key => $column_name) {
                $datas[$key-1][$column_name] = $csv[$column_key];
            }
        }
        $json = json_encode($datas, JSON_PRETTY_PRINT);
        fclose($handle);
        print_r($json);
    }
}

Upvotes: 0

Views: 73

Answers (1)

Barmar
Barmar

Reputation: 782181

Don't call json_encode() inside the loop. Initialize $datas outside the loop, and encode it after the loop.

$datas = [];

foreach($arr as $date) {
    $url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports_us/".$date.".csv";

    if (($handle = fopen($url, "r")) !== FALSE) {
        $csvs = [];
        while($row = fgetcsv($handle)) {
            $csvs[] = $row;
        }
        $column_names = [];
        // Use header line to get array keys
        foreach ($csvs[0] as $single_csv) {
            $column_names[] = $single_csv;
        }
        array_shift($csvs); // remove header line
        foreach ($csvs as $key => $csv) {
            $new_data = [];
            foreach ($column_names as $column_key => $column_name) {
                $new_data[$column_name] = $csv[$column_key];
            }
            $datas[] = $new_data;
        }
        fclose($handle);
    }
}

$json = json_encode($datas, JSON_PRETTY_PRINT);
print_r($json);

Upvotes: 1

Related Questions