How to convert json data in json file in Laravel 5.6
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Convert JSON Data into a File in Laravel: A Developer's Guide
As developers working with Laravel, we frequently deal with data manipulation—taking structured results from our database and presenting them in various formats. Often, this involves serializing data into JSON for API responses. However, sometimes the requirement shifts: instead of sending the JSON over an HTTP response, you need to persist that data directly into a physical file on the server disk.
This guide will walk you through the most robust and practical way to take your Eloquent data (like an array of movies) and convert it into a movies.json file within your Laravel application structure.
The Scenario: From Database to File
You are starting with this standard Laravel query:
$movies = Movie::all();
return response()->json($movies);While the above code is perfect for an API endpoint, we need a different approach when the goal is file persistence rather than an HTTP response. We want to write the $movies collection into a file named movies.json.
Step-by-Step Implementation
To achieve this, you need to leverage PHP's file handling functions within your Laravel controller or service layer. The key decisions here involve where to save the file and how to structure the JSON content.
1. Determine the File Location (Best Practice)
A critical decision is where to place the file. Never write user-facing data directly into the public folder. For non-public application files, the Laravel convention dictates using the storage disk. This ensures better security and adheres to Laravel’s architectural principles.
We will use the storage/app/ directory structure for persistence.
2. Writing the JSON Data
Inside your controller method, you will perform three main actions: retrieve data, prepare the file path, and write the content.
Here is a complete example demonstrating how to achieve this using PHP's built-in functions:
<?php
namespace App\Http\Controllers;
use App\Models\Movie;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;
class DataExportController extends Controller
{
public function exportMovies()
{
// 1. Retrieve the data from the database
$movies = Movie::all();
// 2. Prepare the data for file writing (JSON encoding)
$jsonData = json_encode($movies, JSON_PRETTY_PRINT);
// 3. Define the destination path within Laravel's storage directory
$fileName = 'movies.json';
// We write it to the storage disk, typically inside a specific folder structure.
$filePath = storage_path('app/exports/' . $fileName);
// Ensure the directory exists before writing (essential!)
File::ensureDirectoryExists(dirname($filePath));
// 4. Write the JSON string to the file
if (File::put($filePath, $jsonData)) {
return response()->json([
'message' => 'Data successfully exported.',
'file_path' => $filePath
], 200);
} else {
return response()->json(['error' => 'Failed to write file.'], 500);
}
}
}Code Breakdown and Best Practices
$jsonData = json_encode($movies, JSON_PRETTY_PRINT);: We usejson_encode()to convert the PHP array/collection into a JSON formatted string. TheJSON_PRETTY_PRINTflag makes the resulting file human-readable, which is excellent for debugging exported data.- File Path Management (
storage_path('app/exports/' . $fileName)): Using Laravel'sstorage_path()function is superior to manually constructing paths because it reliably points to the correct location on the filesystem, regardless of your application's root directory configuration. We create a dedicated subfolder (exports) for organized file management. - Directory Check (
File::ensureDirectoryExists(...)): This step prevents fatal errors if theexportsdirectory does not yet exist when the script runs. Always check and ensure your directories are ready before attempting to write files.
Conclusion: Embracing Laravel's Ecosystem
Converting dynamic data into static files is a common requirement in backend development, whether you are generating reports, exporting data for third-party systems, or creating static assets. By utilizing Laravel’s built-in file handling capabilities—specifically the Illuminate\Support\Facades\File facade—you ensure your application remains secure and adheres to best practices.
Remember, when building scalable applications, leveraging frameworks like Laravel allows you to focus on business logic while the framework handles the complexities of filesystem interaction and security. For more insights into maintaining clean code and robust data handling within the Laravel ecosystem, always refer back to resources like Laravel Company. Happy coding!