MrAndySweet
MrAndySweet

Reputation: 103

How to merge 2 JSON files into one JSON FeatureCollection

I have two JSON files each containing a FeatureCollection with the same structure but different data. I'm trying to combine them into a single JSON file as a single FeatureCollection containing all the data. I've managed to almost do this but "FeatureCollection" is being repeated at the start of the file making it invalid JSON.

I think it is something to do with the way I'm JSON encoding the two files individually and then again when I'm combining them but after a day of trying various combinations I haven't managed to figure it out.

This is the code which creates the first JSON file (where $results is generated by a database query):

$geojson = array( 'type' => 'FeatureCollection', 'features' => array());

while ( $results->fetch() ) {
    $feature = array(
        'type' => 'Feature', 
              'properties' => array(
            'name' => $results->field('name')
            ),
      'geometry' => array(
        'type' => 'Point',
        'coordinates' => array((float)$results->field('long'), (float)$results->field('lat'))
            )
        );
    array_push($geojson['features'], $feature);
};

// // Create JSON file
    $fp = fopen('file1.json', 'w');
fwrite($fp, json_encode($geojson));
fclose($fp);

The second file (file2.json) is created in the same way, eg:

$geojson = array( 'type' => 'FeatureCollection', 'features' => array());

while ( $results->fetch() ) {
    $feature = array(
        'type' => 'Feature', 
              'properties' => array(
            'name' => $results->field('name')
            ),
      'geometry' => array(
        'type' => 'Point',
        'coordinates' => array((float)$results->field('long'), (float)$results->field('lat'))
            )
        );
    array_push($geojson['features'], $feature);
};

// // Create JSON file
    $fp = fopen('file2.json', 'w');
fwrite($fp, json_encode($geojson));
fclose($fp);

I'm then combining them using this code:

    $jsonString = file_get_contents('file2.json');
    $jsonString2 = file_get_contents('file1.json');   
    $data = json_decode($jsonString, true);
    $data2 = json_decode($jsonString2, true);

    $op = array_merge_recursive( $data, $data2 );

    $fp = fopen('file3.json', 'w');
    fwrite($fp, json_encode($op));
    fclose($fp); 

The resulting file is mainly fine, it contains all the data that I require and is properly formatted apart from the fact that at the start of the file it has:

{"type":["FeatureCollection","FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc

Instead of:

{"type":["FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc

I can't figure out why there are two instances of "FeatureCollection" at the beginning or how to only produce one.

Upvotes: 0

Views: 292

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

When you merge the two sets of data, both with their own copy of "type" in, the merge will create an output containing both items.

Consider...

$data = [ "type" => "FeatureCollection"];
$data2 = [ "type" => "FeatureCollection"];

$op = array_merge_recursive( $data, $data2 );

print_r($op);

This will output

Array
(
    [type] => Array
        (
            [0] => FeatureCollection
            [1] => FeatureCollection
        )

)

As this is the two source arrays combined.

A simple way of resolving this would be to re-set the value in the array, this code just picks the first value and sets it to that...

$op["type"] = $op["type"][0];
print_r($op);

will give...

Array
(
    [type] => FeatureCollection
)

Upvotes: 1

Related Questions