Laravel 8 Create JSON Text File for Download using File and Response

Last updated on by Digamber

Throughout this tutorial, you will find out how to create a JSON Text File for Download in Laravel 8 application.

Laravel offers a sublime ecosystem, which solves tons of your problems in advance. For instance, if you require to create HTML, text, CSS, PHP, and JSON files, you don’t need to rely on third-party libraries. You can handle this specific problem by using the File Facade.

The File facade is an impeccable class that offers an innumerable solution to handle Files in laravel; consequently, it can help you create, update or delete a file. Through this quick guide, we will show you how to create a JSON file in Laravel effortlessly.

We will also learn how to download the file we create with the File facade. Not only but also we will check out the usage of the Response class; it mainly helps in downloading from the created file.

The following method goes into the JsonController, also use Response, View, and File classes at the top of the controller.

namespace App\Http\Controllers;

use Response;
use View;
use File;

class JsonController extends Controller

	public function jsonFileDownload()
	  $data = json_encode(['Text One', 'Text Two', 'Text Three']);
      $jsongFile = time() . '_file.json';

	  File::put(public_path('/upload/json/'.$jsongFile), $data);

	  return Response::download(public_path('/upload/jsonfile/'.$jsongFile));


Get inside the routes/web.php and add the code to create a route:

Route::get('json-file-download', array('as'=> 'jsonFileDownload', 'uses' => 'JsonController@jsonFileDownload'));