Laravel 10 – Search Through JSON File And Remove Duplicates

Laravel 10 built on PHP basically. So we can manipulate Arrays and file handling functions for JSON. Here’s an example of how you might use this function in a Laravel controller to search for data in a JSON file:

public function searchData($keyword) {
    $json = file_get_contents(public_path('data.json'));
    $data = json_decode($json, true);
    
    $results = [];
    foreach ($data as $item) {
        if (strpos($item['title'], $keyword) !== false) {
            $results[] = $item;
        }
    }
    
    return view('search_results', ['results' => $results]);
}

In Laravel 10, we use the file_get_contents function to read the contents of the data.json file into a string. Then, we use json_decode to convert the JSON string into an associative array.

Next, we loop through the array and check each item’s title property to see if it contains the keyword. If it does, we add the item to the $results array.

Finally, we return a view and pass the $results array as a parameter. The view can then display the search results to the user.

If you want to eliminate duplicates because JSON file must not have duplicate keys, then you can use the array_unique function to eliminate duplicate keys in a JSON file. Here’s an example of how you might use this function to remove duplicate keys from a JSON file in Laravel:

public function removeDuplicates() {
    $json = file_get_contents(public_path('data.json'));
    $data = json_decode($json, true);
    
    $data = array_map("unserialize", array_unique(array_map("serialize", $data)));
    
    $json = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents(public_path('data.json'), $json);
}

In this example, we first use the file_get_contents function to read the contents of the data.json file into a string. Then, we use json_decode to convert the JSON string into an associative array.

Next, we use array_map and array_unique functions to eliminate duplicates in the associative array. The array_map function is used to apply the serialize function to each element of the array, and the array_unique function is used to remove duplicates based on the serialized representation of the elements.

Finally, we use the json_encode function to convert the cleaned array back into a JSON string, and use file_put_contents to save the new JSON string back to the data.json file.

Now in the same way you can add duplicate records at the end of the JSON file. You can use the file_get_contents and file_put_contents functions to append records to the end of a JSON file. Here’s an example of how you might use these functions to append records to a JSON file in Laravel:

public function appendRecord($record) {
    $json = file_get_contents(public_path('data.json'));
    $data = json_decode($json, true);
    
    $data[] = $record;
    
    $json = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents(public_path('data.json'), $json);
}

Now we first use the file_get_contents function to read the contents of the data.json file into a string. Then, we use json_decode to convert the JSON string into an associative array.

Next, we use the [] syntax to append the new record to the end of the array.

Finally, we use the json_encode function to convert the updated array back into a JSON string, and use file_put_contents to save the new JSON string back to the data.json file.